【问题标题】:Uncaught Error: Unsupported operation: ProcessUtils._sleep未捕获的错误:不支持的操作:ProcessUtils._sleep
【发布时间】:2020-07-04 18:27:48
【问题描述】:
import 'dart:io';

void main() {
 performTask();
}

void performTask() {
 task1();
 task2();
 task3();
}

void task1() {
 print('task1');
}

void task2() {
 Duration timeDuration = Duration(seconds: 3);
 sleep(timeDuration);
 print('task2');
}

void task3() {
 print('task3');
}

在执行第一个函数 task1() 后,它会抛出一个错误:

Uncaught Error: Unsupported operation: ProcessUtils._sleep

【问题讨论】:

  • 你是如何执行这段代码的?
  • 在 dartpad.dev 中在线
  • dartpad.dev 不支持“dart:io”。
  • 好的,知道了。顺便说一句

标签: dart


【解决方案1】:

我只是遇到了同样的障碍!不知道如何让 sleep 工作,但我发现使用 async/await 更容易预测:

// Unused import
// import 'dart:io'; // Delete me

void main() {
 performTask();
}

// No need for async/await here, just the method in which it's used to await Future.delayed
void performTask() {
 task1();
 task2();
 task3();
}

void task1() {
 print('task1');
}

// I'm still a bit new to flutter, but as I understand it, best practice is to use Future<T> myFunction() {...} when defining async/await method.
// In this case <T> is <void> because you're not returning anything!
Future<void> task2() async {
 Duration timeDuration = Duration(seconds: 3);
 // sleep(timeDuration) // Delete Me
 await Future.duration(timeDuration); // replacement for the sleep method, comes from the 'package:flutter/material.dart'
 print('task2');
}

void task3() {
 print('task3');
}

信用:How can I "sleep" a Dart program

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多