Flutter/Dart - Difference between () {} and () => {}

前端 未结 6 2150
挽巷
挽巷 2021-02-13 03:44

In Flutter/Dart the examples sometimes show fat arrow and sometimes dont. Here are examples:

RaisedButton(
  onPressed: () {
    setState(() {
      _myTxt = \"         


        
6条回答
  •  攒了一身酷
    2021-02-13 04:42

    => is used to return a value of an anonymous function.

    () {} lets you execute multiple statements.

    while

    () => {myVar} or () => myVar; allows one single statement.

    () => myVar; is short and simple when returning one statement.


    The same logic goes for creating non anonymous functions too.

    Single statement func func() => y = x + x;

    Multiple statement func

    func () {
       x = x + x; 
       print(x + ' value of x');
    };
    

提交回复
热议问题