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

前端 未结 5 1348
情歌与酒
情歌与酒 2020-12-03 15:11

I know the syntax for calling a function after onPressed and onTap for a widget. There are two options We can use either the () => functio

相关标签:
5条回答
  • 2020-12-03 15:42

    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
    
    0 讨论(0)
  • 2020-12-03 15:44

    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

    0 讨论(0)
  • 2020-12-03 15:49

    () => 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.
    0 讨论(0)
  • 2020-12-03 15:51

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

    0 讨论(0)
  • 2020-12-03 15:56

    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));
      }
    }
    
    0 讨论(0)
提交回复
热议问题