【问题标题】:What is the difference between calling the function without parentheses and with parentheses调用不带括号和带括号的函数有什么区别
【发布时间】:2020-04-17 05:44:30
【问题描述】:

在 onPressed 或 Ontap 上调用不带括号和带括号的函数有什么区别?

我只知道在onPressed上不能用括号调用void函数。

floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: Icon(Icons.add),
  ),

_incrementCounter 的返回类型为 void

void _incrementCounter() {
    setState(() {
        _counter++;
    });  
}

但我没有找到任何合适的文档。

【问题讨论】:

  • 当你写_incrementCounter时你实际上并没有调用它——你只是说按下按钮时会调用_incrementCounter函数
  • @pskink 是的,当然,在按下按钮时调用一个函数。
  • 再次onPressed: _incrementCounter 不是"calling the function without parentheses"
  • 好的,你正在接受回调上下文。

标签: flutter dart


【解决方案1】:

简单来说,在 onPressed 中,我们可以传递函数引用或匿名函数之类的回调,因此当用户单击此特定小部件时,该函数将引用并执行,但通常在构建该特定小部件时直接执行带括号的函数。

【讨论】:

    【解决方案2】:
    void _incrementCounter() {
        setState(() {
            _counter++;
        });  
    }    
    
    floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    

    * 如我所见,在您的代码中,函数的返回类型为 void。在颤振中,如果返回类型为 void 并且函数定义在同一个类中,其中定义的函数在小部件中被调用,那么我们不使用括号。在flutter中,我们只需使用括号调用函数......它最终充当一个指针,该指针将指向返回类型为void的函数,并在小部件中调用函数的同一类中定义*

    【讨论】:

      【解决方案3】:

      使用括号意味着你给函数一个空的参数集来运行。如果函数需要0个参数,使用括号和函数名将执行函数。

      另一方面,没有参数意味着你只是提到了那个函数。喜欢,

      8;//Here I'm just mentioning 8. It's useless
      

      例子:

      void fun(){
        print('Func is running!');
      }
      
      void main(){
      func();//Here i wanna execute 'func'
      Function a = func;//Here i wanna assign 'func' to a
      }
      

      输出:

      函数正在执行

      【讨论】:

        【解决方案4】:

        _incrementCounter里面onPressed是一个函数引用,基本意思是它不是立即执行的,它是在用户点击特定的widget之后执行的。(回调)

        _incrementCounter()是一个函数调用,它会立即执行。

        因此,在onPressed 中,您可以传递函数引用或充当回调的匿名函数。

        floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        

        floatingActionButton: FloatingActionButton(
            onPressed: () {
                // Add your onPressed code here!
              },
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        

        这不是 dart 特有的,它也用javascript 和许多其他语言完成:

        What is the difference between a function call and function reference?

        Javascript function call with/without parentheses

        【讨论】:

          【解决方案5】:

          incrementCounter 是对该函数的引用。您将函数作为参数传递,以便稍后在某处调用。如果您有子小部件,这通常用作回调函数

          incrementCounter() 将调用函数调用。对于这种情况,您的计数器会在小部件构建时自动加 1,这是您不希望发生的。

          而且通常直接调用函数是不对的,应该这样写:

          onPressed: () {
             _incrementCounter();
          },
          

          onPressed: () => _incrementCounter();
          

          【讨论】:

          • "And usually it is not right to call the function directly" 我不这么认为 - 这完全没问题,为什么要引入另一个匿名函数只是为了间接调用另一个函数?检查颤振源,你会发现这里到处都使用了直接方法访问
          • 无论如何我们都可以调用 onPressed: incrementCounter() 吗?
          • @pskink 我将不得不看看它是否可能这样,但我认为你是对的。
          • @M.ArslanKan 你可以调试你的函数,看看当你触发 onPressed 时它是否在里面。尝试一个不使用创建闭包的方法。
          【解决方案6】:

          这里有区别:

          onPressed: _incrementCounter 是对现有函数的引用。

          这只有在 onPressed_incrementCounter 预期的回调参数兼容时才有效。

          onPressed: _incrementCounter() _incrementCounter() 被执行并将返回结果传递给onPressed。这是一个常见的错误,当实际上是为了传递对 _incrementCounter 的引用而不是调用它时,无意中完成的。

          【讨论】:

          • 你能否解释一下你的说法“这只有在 onPressed 和 _incrementCounter 预期的回调参数兼容的情况下才有效。”
          • 我认为我们不能调用 onPressed: _incrementCounter() (它们之间没有 => 粗箭头)返回结果。我的意思是它的语法错误。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-27
          • 2021-06-29
          相关资源
          最近更新 更多