How can I “sleep” a Dart program

前端 未结 7 2221
梦谈多话
梦谈多话 2020-12-12 23:39

I like to simulate an asynchronous web service call in my Dart application for testing. To simulate the randomness of these mock calls responding (possibly out of order) I\

相关标签:
7条回答
  • 2020-12-13 00:02

    2019 edition:

    In Async Code

    await Future.delayed(Duration(seconds: 1));
    

    In Sync Code

    import 'dart:io';
    
    sleep(Duration(seconds:1));
    

    Note: This blocks the entire process (isolate), so other async functions will not be processed. It's also not available on the web because Javascript is really async-only.

    0 讨论(0)
提交回复
热议问题