dart assert语句的一个很好的解释

时间:2019-12-06 05:32:36

标签: flutter dart

此代码来自flutter sdk。

我听不懂assert((){}())的表达方式。

它来自setState()函数。

  assert(() {
          if (result is Future) {
            throw FlutterError(
              'setState() callback argument returned a Future.\n'
              'The setState() method on $this was called with a closure or method that '
              'returned a Future. Maybe it is marked as "async".\n'
              'Instead of performing asynchronous work inside a call to setState(), first '
              'execute the work (without updating the widget state), and then synchronously '
              'update the state inside a call to setState().'
            );
          }
          // We ignore other types of return values so that you can do things like:
          //   setState(() => x = 3);
          return true;
        }());

我已经测试了以下代码。

b未打印。

main() {
  assert(() {
    print("b");
  } ());
  print("a");
}

0 个答案:

没有答案