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.
When we call a function with parentheses, the function gets execute and returns the result to the callable. In another case, when we call a function without parentheses, a function reference is sent to the callable rather than executing the function itself.
Using a function to do a particular task any point in program is called as function call. So the difference between the function and function call is, A function is procedure to achieve a particular result while function call is using this function to achive that task.
Parentheses are used to enclose incidental or extra information, such as a passing comment, a minor example or addition, or a brief explanation. The writer may choose to put additional information within parentheses or to set off the text using dashes or commas.
It means you can call a function without parentheses if that function is no parameters. otherwise you need to include parentheses if it have parameters.
_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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With