sleep 函数

void sleep(
  1. Duration duration
)

暂停指定为 duration 的持续时间。

请小心使用此功能,因为当在 sleep 调用中被阻塞时,isolate 不能处理任何异步操作。

var duration = const Duration(seconds: 5);
print('Start sleeping');
sleep(duration);
print('5 seconds has passed');

实现

void sleep(Duration duration) {
  int milliseconds = duration.inMilliseconds;
  if (milliseconds < 0) {
    throw new ArgumentError("sleep: duration cannot be negative");
  }
  if (!_EmbedderConfig._maySleep) {
    throw new UnsupportedError(
        "This embedder disallows calling dart:io's sleep()");
  }
  _ProcessUtils._sleep(milliseconds);
}