How to define onPressed property for button - flutter

What is the difference for these two variants? If the function _getCsvDocunent is just void type then what does it mean if we define onPressed like onPressed: () => _getCsvDocunent(), ?
FlatButton(
child: Text('Provide .csv data file'),
onPressed: _getCsvDocunent,
),
vs
FlatButton(
child: Text('Provide .csv data file'),
onPressed: () => _getCsvDocunent(),
),

In onPressed:_getCsvDocunent: onPressed gets an reference to _getCsvDocument passed to it. This way of passing a reference is only possible when the function to be passed is already defined and is compatible with onPressed.
In onPressed: () => _getCsvDocunent(): onPressed gets the value returned by
_getCsvDocunent after it is done executing.
In onPressed: () => _getCsvDocunent(): onPressed gets a reference to _getCsvDocument exactly similar to the case in the first bullet point, difference being, in this case a function reference (_getCsvDocument) is directly passed to onPressed instead of using an inline function to return a reference to _getCsvDocument.

Related

What is the difference between calling a method with and without parantheses in flutter? [duplicate]

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

Flutter - Can only pop AlertDialog OR execute passed function - need to do both

TL;DR: Trying to pass a function call to a custom AlertDialog. AlertDialog needs to be popped after the function -> can't make it work.
I've created a custom AlertDialog to use throughout my app. It looks something like this:
customAlertDialog({required context, buttonAction}){
showDialog(context: context, builder: (context) => AlertDialog(
title: Text("example title", style: TextStyle(color: AppTheme.colors.white),),
content: const Text("some content"),
actions: [
TextButton(onPressed: () {Navigator.of(context).pop();}, child: Text(
"Abbrechen",
style: TextStyle(
color: AppTheme.colors.white),
),),
TextButton(
child: Text("do something",
style: TextStyle(
color: AppTheme.colors.lightRed),
),
onPressed: buttonAction)
],
),);
}
The customAlertDialog takes a function call as an argument (here named buttonAction) for the last TextButtons onPressed action. It works fine when I pass:
buttonAction: () => deleteUser(context)
The problem is that this does not work in combination with a pop method. In the following only deleteUser will be called:
buttonAction: () => [deleteUser(context), Navigator.of(context).pop()]
the same if written like this:
buttonAction: () {deleteUser(context), Navigator.of(context).pop()}
I guess that the context of the customAlertDialog itself needs to be popped. So I've tried the following in the customAlertDialog (buttonAction contains () => deleteUser(context):
onPressed: () => [buttonAction, Navigator.of(context).pop()]
Now it only pops the dialog, probably because dart cannot interpret the buttonAction. So I've searched to find a way to pass only the function that should be called but wasn't successful doing so.
So my question is: How can I pass a function and still pop the dialog?
EDIT:
As #SlowDeepCoder mentioned the problem could have been that the Navigator.pop() method throws the context from the stack before deleteUser() has finished and therefore it doesn't work. It was tried to be fixed with the following but it did not work:
buttonAction: () async{ await deleteUser(context); Navigator.of(context).pop(); }
This is because you call pop() on a Navigator from an incorrect BuildContext. So instead of
buttonAction: () {
deleteUser(context);
Navigator.of(context).pop();
}
you have to do something like this:
buttonAction: (innerContext) {
deleteUser(innerContext); // not sure if you need the innerContext here. Depends on your other app code
// deleteUser(context); // use this line if the above does not work
Navigator.of(innerContext).pop();
}
together with
TextButton(
child: Text("do something"),
onPressed: () => buttonAction(context),
)
I also would recommend you to give bot of your BuildContexts (customAlertDialog and showDialog) different names.
You do not always have to use arrow function to pass a custom function.
You can simply pass the function as
buttonAction: (){
deleteUser(context);
Navigator.of(context).pop();
}

The method 'save' was called on null. Receiver: null Tried calling: save() [duplicate]

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

Functions in Flutter

I was playing around with functions and tried to create a new widget:
newWidget(Function onTap){
return InkWell(
...
onTap: () => onTap
);
}
When tried to use it, I found that giving the onTap as onTap: () => onTap, and using it as onTap: () => onTap(), performed differently. And there's other ways to use the onTap property.
So what's the difference between:
onTap: () => onTap
onTap: () => onTap()
onTap: onTap()
onTap: () => func
This one point the widget to func function and tells it run it when tapped. Like you give it the address of the function and tell it to go there in case someone taps.
onTap: () => func()
onTap: func()
These two basically do the same thing and give onTap property the value returned from the func(). These ones however, takes onTap to the address instead of pointing it to it. These will run immediately after build completes.
You can simply call onTap: func if you don't have to pass it any parameters.
Otherwise call onTap: (param1, param2, ...) => func(param1, param2, ...)
You can also use it like:
onTap: funcationName //with out brackets

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

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