_incrementCounter
inside onPressed
是一个函数引用,这基本上意味着它不会立即执行,而是在用户单击特定小部件后执行。(回调)
_incrementCounter()
是一个函数调用并且立即执行。
因此,内部onPressed
您可以传递函数引用或充当回调的匿名函数。
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
or
floatingActionButton: FloatingActionButton(
onPressed: () {
// Add your onPressed code here!
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
这不是 dart 特有的东西,它也是在javascript
以及许多其他语言:
函数调用和函数引用有什么区别? https://stackoverflow.com/questions/15886272/what-is-the-difference-between-a-function-call-and-function-reference
带/不带括号的 JavaScript 函数调用 https://stackoverflow.com/questions/34563330/javascript-function-call-with-without-parentheses