调用不带括号的函数和带括号的函数有什么区别 [英] What is the difference between calling the function without parentheses and with parentheses
问题描述
在onPressed或Ontap上不带括号调用函数和带括号的函数有什么区别?
What is the difference between calling the function without parentheses and with parentheses on onPressed or Ontap?
我只知道不能在带括号的情况下调用void函数
I just know that void function can't be called with parentheses on onPressed.
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
_incrementCounter
具有无效的返回类型
void _incrementCounter() {
setState(() {
_counter++;
});
}
但是我没有找到任何合适的文档。
But I didn't find any proper documentation.
推荐答案
_incrementCounter
在 onPressed
内是函数引用,基本上意味着它不会立即执行,而是在用户单击特定的窗口小部件之后执行。(回调)
_incrementCounter
inside onPressed
is a function reference, which basically means it is not executed immediately, it is executed after the user clicks on the specific widget.(callback)
_incrementCounter( )
是一个函数调用,它会立即执行。
_incrementCounter()
is a function call and it is executed immediately.
因此,在 onPressed
内您可以传递函数引用或充当回调的匿名函数。
Therefore, inside onPressed
you can either pass a function reference or an anonymous function that will act as a callback.
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
或
floatingActionButton: FloatingActionButton(
onPressed: () {
// Add your onPressed code here!
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
the不是Dart特有的东西,它也可以用 javascript
和许多其他语言完成:
The is not something specific to dart, it is also done in javascript
and many other languages:
这篇关于调用不带括号的函数和带括号的函数有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!