首页 > 解决方案 > dart assert 声明的一个很好的解释

问题描述

此代码来自fluttersdk。

我无法理解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");
}

标签: flutterdart

解决方案


推荐阅读