Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the empty parentheses after the onPressed property mean in Dart?

Tags:

flutter

dart

I know the syntax for calling a function after onPressed and onTap for a widget. There are two options We can use either the () => function() or the () { function(); } syntax. What do the empty parentheses mean?

like image 372
flutter Avatar asked Sep 19 '18 10:09

flutter


People also ask

What does onPressed mean?

onPressed: myFunction a reference to an existing function is passed. This only works if the parameters of the callback expected by onPressed and myFunction are compatible. onPressed: myFunction() myFunction() is executed and the returned result is passed to onPressed .

How do you use onPressed in flutter?

Flutter RaisedButton's onPressed property lets you assign it with a callback function. The application executes this callback function when user presses on the RaisedButton. In this tutorial, we will learn how to execute a set of statements using callback function for onPressed property of RaisedButton.

What is anonymous function in DART?

An anonymous function behaves the same as a regular function, but it does not have a name with it. It can have zero or any number of arguments with an optional type annotation. We can assign the anonymous function to a variable, and then we can retrieve or access the value of the closure based on our requirement.


Video Answer


4 Answers

() => expression or () { statements } creates a closure or inline function.

This way you create inline a function that is passed as argument to be called in case of the event onPressed by the widget you pass it to.

The expression or statements have the context where they were created available and can access and use all members and identifiers available in that context (variables, methods, functions, typedefs, ...).

If you use

  • onPressed: myFunction a reference to an existing function is passed.
    This only works if the parameters of the callback expected by onPressed and myFunction are compatible.
  • onPressed: myFunction() myFunction() 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 myFunction instead of calling it.
like image 61
Günter Zöchbauer Avatar answered Oct 23 '22 15:10

Günter Zöchbauer


If I understand your question correctly, you are asking about the bolded one () => function().

With that assumption I am trying to answer.

onTap, onPressed are the taking function as arguments. Possible values can be

func callbackFunction() {
 // what ever we want to do onTap
}

1. onTap: callbackFunction

2. onTap: () => callbackFunction() // onTap: callbackFunction() it will invoke the method while building itself. 
                                   // So we are making it lazy by wrapping in another anonymous function. 

3. onTap: () { callbackFunction(); }

4. onTap: () => print("tapped")   // anonymous one line function

5. onTap: () { print("tapped"); 
               // what ever we want to do onTap
               print("tapped"); 
           }   // anonymous multiline function
like image 33
Dinesh Balasubramanian Avatar answered Oct 23 '22 16:10

Dinesh Balasubramanian


They are not the same thing. According the the language docs, the fat arrow is syntactical sugar for a return statement.

https://www.dartlang.org/guides/language/language-tour#functions

() => function()

is comparable to this line

(){ return function(); }

not this statement

() { function(); } //returns void

I guess you got away with it because both handlers have a tendency to be void.

https://docs.flutter.io/flutter/dart-ui/VoidCallback.html

https://docs.flutter.io/flutter/gestures/GestureTapCallback.html

https://docs.flutter.io/flutter/material/ListTile/onTap.html

https://docs.flutter.io/flutter/material/IconButton/onPressed.html

void main() {
  num add(a,b) => a + b;
  num add_void(a,b) { a+b; }
  for (int i = 0; i < 5; i++) {
    print('hello ${i + 1}');
    print(add(i,i));
    print(add_void(i,i));
  }
}
like image 27
user1462442 Avatar answered Oct 23 '22 15:10

user1462442


I've learned to treat the empty parentheses as a build first then with the returned value execute this function.

I ran into an issue using the second method you posted where flutter would crash stating "cannot build because the framework is already building" and found this post on StackOverflow which may give you a better idea of what it means. Flutter - Cannot build because the frawework is already building

like image 44
F-1 Avatar answered Oct 23 '22 17:10

F-1