2015-08-24 101 views

回答

4

参见作为Dart 1.22assert()带有一个可选的消息。

assert(configFile != null, "Tool config missing."); 

如果断言失败,它会产生类似以下内容:

Unhandled exception: 
'file:///.../main.dart': Failed assertion: line 9 pos 10: 
'configFile != null': Tool config missing. 
#0  _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:33) 
#1  _AssertionError._throwNew (dart:core-patch/errors_patch.dart:29) 
#2  main (file:///.../main.dart:9:10) 

注意,错误消息中包含的实际断言(configFile != null)。

4

有一个悬而未决的问题有一个解决办法https://github.com/dart-lang/sdk/issues/6190#issuecomment-119103626

assert(() => test || throw "message"); 

我试过,但这种方式是行不通的。稍微修改工作版本

var test = false; 
assert(test ? true : throw "message");