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

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

    There seems to be one difference at least in case of Dart version 2.10:

    If the expression to be executed is a Future, then the execution order is not the same.

    =>

    new Future(() => print('future #1 of 2'))
      .then((_) => new Future(() => print('future #1a (a new future)')))
      .then((_) => print('future #1b'));
    
    new Future(() => print('future #2 of 2'))
      .then((_) => new Future(() => print('future #2a (aa new futue)' )))
      .then((_) => print('future #2b'));
    

    The result is:

    future #1 of 2
    future #2 of 2
    future #1a (a new future)
    future #1b
    future #2a (aa new futue)
    future #2b`
    

    {} :

    new Future(() => print('future #1 of 2'))
      .then((_) => new Future(() => print('future #1a (a new future)')))
      .then((_) => print('future #1b'));
    
    new Future(() => print('future #2 of 2'))
      .then((_) { new Future(() => print('future #2a (aa new futue)' )); })
      .then((_) => print('future #2b'));
    

    The result is

    future #1 of 2
    future #2 of 2
    future #2b
    future #1a (a new future)
    future #1b
    future #2a (a new futue)
    

提交回复
热议问题