What is the difference between calling the function without parentheses and with parentheses

前端 未结 5 875
甜味超标
甜味超标 2021-01-21 08:47

What is the difference between calling the function without parentheses and with parentheses on onPressed or Ontap?

I just know that void function can\'t be called with

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 09:51

    incrementCounter is a reference to the function. You're passing the function along as a parameter to be invoked somewhere later. This is usually used as callback function, if you have a child widget

    incrementCounter() will invoke the function call. For this case, your counter will automatically add 1 when the widget builds, which you don't want to happen.

    And usually it is not right to call the function directly, you should write it like this:

    onPressed: () {
       _incrementCounter();
    },
    

    OR

    onPressed: () => _incrementCounter();
    

提交回复
热议问题