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

前端 未结 5 1377
情歌与酒
情歌与酒 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
    

提交回复
热议问题