[英]How should i use Dart Isolate unhandledExceptionCallback?
[英]how should I use assert in Dart?
我看到了類似的示例代碼:
class ModelBinding extends StatefulWidget {
ModelBinding({
Key key,
this.initialModel = const GalleryOptions(),
this.child,
}) : assert(initialModel != null),
super(key: key);
...
所以我寫了一些東西:
class Person {
String firstName;
Person({name}){
print(name);
}
}
class Employee extends Person {
Employee(String name) : assert(false), super(name: name);
}
main() {
var emp = new Employee('Jason');
}
不管是assert(false)
還是assert(true)
,結果都是一樣的。
那么assert
的含義是什么?
assert
用於調試,它只是意味着條件應該為true
才能繼續。 讓我解釋:
class MyClass {
final int age;
MyClass({this.age});
void someMethod() {
// using `age` here
}
}
如果age
是null
,您可能會在someMethod
中遇到問題,因此要確保它不是null
,您可以使用如下assert
:
class MyClass {
final int age;
MyClass({this.age}) : assert(age != null, "Make sure age isn't null");
void someMethod() {
// using `age` here
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.