Dart中onPressed属性后面的空括号是什么意思? [英] What does the empty parentheses after the onPressed property mean in Dart?

查看:153
本文介绍了Dart中onPressed属性后面的空括号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 onPressed onTap 之后为小部件调用函数的语法。有两种选择,我们可以使用()=> function()(){function(); } 语法。空括号是什么意思?

I know the syntax for calling a function after onPressed and onTap for a widget. There are two options We can use either the () => function() or the () { function(); } syntax. What do the empty parentheses mean?

推荐答案

()=>表达式(){语句} 创建一个闭包或内联函数。

() => expression or () { statements } creates a closure or inline function.

此创建内联函数的方式,该函数作为传递给参数的函数传递,以防在您将其传递给小部件的事件 onPressed 时发生。

This way you create inline a function that is passed as argument to be called in case of the event onPressed by the widget you pass it to.

表达式语句具有创建它们的上下文,并且可以访问和使用所有在这种情况下可用的成员和标识符(变量,方法,函数,typedef,...)。

The expression or statements have the context where they were created available and can access and use all members and identifiers available in that context (variables, methods, functions, typedefs, ...).

如果使用


  • onPressed:myFunction 传递了对现有函数的引用。

    仅在以下参数有效 onPressed myFunction 预期的回调是兼容的。

  • onPressed:执行myFunction() myFunction(),并将返回的结果传递给 onPressed 。这是一个常见的错误,当无意间执行时实际上是要传递对 myFunction 的引用而不是调用它。

  • onPressed: myFunction a reference to an existing function is passed.
    This only works if the parameters of the callback expected by onPressed and myFunction are compatible.
  • onPressed: myFunction() myFunction() is executed and the returned result is passed to onPressed. This is a common mistake when done unintentionally when actually the intention was to pass a reference to myFunction instead of calling it.

这篇关于Dart中onPressed属性后面的空括号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆