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

前端 未结 6 2089
孤独总比滥情好
孤独总比滥情好 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:36

    I found that the mean the exact same thing. The only difference is that you can use (you don't have to) the fat arrow if there is only one statement. Following is the above RaisedButton declaration with the fat arrow. Notice I had to remove two curly braces and one semi-colon:

    RaisedButton(
      onPressed: () {
        setState(() =>
          _myTxt = "Text Changed"
        );
      },
    

    If you are used to other languages that allow you to put multiple statements after a fat arrow you'll you'll find that you can't in dart and if you try you'll get an error as in the following:

    this wont work

    RaisedButton(
      onPressed: () {
        setState(() => {
          _myTxt = "Text Changed";
          _myTxt = "Never Mind";
        });
      },
    

提交回复
热议问题