How can I “sleep” a Dart program

前端 未结 7 2237
梦谈多话
梦谈多话 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-12 23:57

    You can also use the Future.delayed factory to complete a future after a delay. Here is an example of two functions that return a string asynchronously after a delay:

    import 'dart:async';
    
    Future sleep1() {
      return new Future.delayed(const Duration(seconds: 1), () => "1");
    }
    
    Future sleep2() {
      return new Future.delayed(const Duration(seconds: 2), () => "2");
    }
    

提交回复
热议问题