What does the empty parentheses after the onPresse

2020-01-29 11:33发布

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?

标签: dart flutter
5条回答
够拽才男人
2楼-- · 2020-01-29 12:14

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
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-01-29 12:16

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

查看更多
何必那么认真
4楼-- · 2020-01-29 12:19

=> (fat arrow) syntax is handy for functions that contain a single statement. This syntax is especially useful when passing anonymous functions as arguments.

查看更多
家丑人穷心不美
5楼-- · 2020-01-29 12:30

() => 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.
查看更多
老娘就宠你
6楼-- · 2020-01-29 12:32

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));
  }
}
查看更多
登录 后发表回答