Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我应该如何在Dart中使用断言?_Dart - Fatal编程技术网

我应该如何在Dart中使用断言?

我应该如何在Dart中使用断言?,dart,Dart,我看到exmaple代码类似于: class ModelBinding extends StatefulWidget { ModelBinding({ Key key, this.initialModel = const GalleryOptions(), this.child, }) : assert(initialModel != null), super(key: key); ... 所以我写了一些东西: class Person {

我看到exmaple代码类似于:

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
passed为
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
  }
}

它与其他编程语言(例如,,…)中的相同。您没有看到任何效果,因为您可能正在运行未启用断言的代码发布版本。改为运行调试生成。