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

前端 未结 6 2095
孤独总比滥情好
孤独总比滥情好 2021-02-13 03:36

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

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


        
6条回答
  •  迷失自我
    2021-02-13 04:16

    => 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');
    };
    

提交回复
热议问题