dart: 使用await和不使用await的区别

一,代码

  void myclick() async{

    print("a");
    //await tells dart to wait till this completes.
    // If it's not used before a future,
    // then dart doesn't wait till the future is completed and executes the next tasks/code.
    await Future.delayed(Duration(seconds:
    5),(){
      print("await返回b");
    });
    print("c");
    Future.delayed(Duration(seconds:
    5),(){
      print("不用await返回d");
    });
    print("e");

  }

说明:使用await这个关键字,它告诉Dart等待这个在未来完成,完成之后才转到下一个任务。

 

二,测试效果:

I/flutter (10164): a
I/flutter (10164): await返回b
I/flutter (10164): c
I/flutter (10164): e
I/flutter (10164): 不用await返回d

 

posted @ 2026-03-07 22:16  刘宏缔的架构森林  阅读(1)  评论(0)    收藏  举报