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
_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
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 *
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
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.
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();