6

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 parentheses on onPressed.

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

_incrementCounter has void return type

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

But I didn't find any proper documentation.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
M.ArslanKhan
  • 3,640
  • 8
  • 34
  • 56

6 Answers6

9

_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() is a function call and it is executed immediately.

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),
  ),

or

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

The is not something specific to dart, it is also done in javascript and many other languages:

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

Javascript function call with/without parentheses

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
3

Here is the difference:

onPressed: _incrementCounter is a reference to an existing function is passed.

This only works if the parameters of the callback expected by onPressed and _incrementCounter are compatible.

onPressed: _incrementCounter() _incrementCounter() 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 _incrementCounter instead of calling it.

Sunny
  • 3,134
  • 1
  • 17
  • 31
  • can you please explain more your statement "This only works if the parameters of the callback expected by onPressed and _incrementCounter are compatible." – M.ArslanKhan Dec 27 '19 at 08:17
  • 1
    I think we can't call onPressed: _incrementCounter() (without => fat arrow between them) for return result. I mean its syntax error. – M.ArslanKhan Dec 27 '19 at 08:29
1

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();
fadhli-sulaimi
  • 686
  • 12
  • 17
  • `"And usually it is not right to call the function directly"` i dont think so - this is perfectly OK, why to introduce another anonymous function just to call one another function indirectly? check the flutter sources and you will see that direct method access is used everyhere – pskink Dec 27 '19 at 08:03
  • can we call onPressed: incrementCounter() in any case? – M.ArslanKhan Dec 27 '19 at 08:27
  • @pskink I will have to see if its possible this way, but I think you are right. – fadhli-sulaimi Dec 27 '19 at 09:07
  • @M.ArslanKan You can debug your function and see if its goes inside there when u trigger onPressed. Try one without using creating a closure. – fadhli-sulaimi Dec 27 '19 at 09:07
0
void _incrementCounter() {
    setState(() {
        _counter++;
    });  
}    

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

* As I see that in your code the return type is void for the function. in flutter if the return type is void and the function is defined in the same class, where the defined function is being called in the widget then we don't use parentheses. in a flutter, we simply call the function with using parentheses ...... which ultimately act as a pointer which will point towards function with return type void and defined in the same class in which function is called in widget *

0

In simple word In onPressed we can pass callback like function reference or anonymous function so when user click on this specific widget then the function will refer and execute but normally function with parentheses executed directly when build that particular widget.

-2

Using parenthesis means you are giving the function an empty set of parameters to run. Using parenthesis with function name will execute the function if the function requires 0 parameters.

On the other hand, Without parameters means you are just mentioning that function. Like,

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

Example:

void fun(){
  print('Func is running!');
}

void main(){
func();//Here i wanna execute 'func'
Function a = func;//Here i wanna assign 'func' to a
}

Output:

Func is executing

Dummy
  • 47
  • 3