gpt4 book ai didi

flutter - 使用 Timer 而不是 Future.delayed(...) 创建异步等待步骤

转载 作者:行者123 更新时间:2023-12-03 08:03:27 31 4
gpt4 key购买 nike

是否可以使用 Timer 而不是 Future.delayed 执行异步步骤?主要原因是 Timer 类中提供了 cancel() 方法,该方法允许我在 Flutter 中处理小部件时取消正在运行的计时器。

import 'dart:async';

const kFirstStepDuration = Duration(seconds: 1);
const kSecondStepDuration = Duration(seconds: 1);
const kThirdStepDuration = Duration(seconds: 1);

void main() async{
await Future.delayed(kFirstStepDuration);
print('first step');
await Future.delayed(kSecondStepDuration);
print('second step');
await Future.delayed(kThirdStepDuration);
print('end');
}

使用理查德·希普回答后的计时器,我想尽可能取消这些步骤,但下面的代码提示:

first timer to begin
cancel first
cancel second
cancel third

我的期望是提示:

first timer to begin
cancel first
second timer to begin
cancel second
third timer to begin
cancel third
executed in 0:00:00.06000
import 'dart:async';

const kFirstStepDuration = Duration(seconds: 1);
const kSecondStepDuration = Duration(seconds: 1);
const kThirdStepDuration = Duration(seconds: 1);

Timer? firstTimer;
Timer? secondTimer;
Timer? thirdTimer;

final firstCompleter = Completer();
final secondCompleter = Completer();
final thirdCompleter = Completer();

void main() {
threadA();
threadB();
}

void threadA() async {
print('first timer to begin');
firstTimer = Timer(kFirstStepDuration, ()=> firstCompleter.complete());
await firstCompleter.future;

print('second timer to begin');
secondTimer = Timer(kSecondStepDuration, ()=> secondCompleter.complete());
await secondCompleter.future;

print('third timer to begin');
thirdTimer = Timer(kThirdStepDuration, ()=> thirdCompleter.complete());
await thirdCompleter.future;
}

void threadB() async {
await Future.delayed(Duration(milliseconds: 20));
firstTimer?.cancel();
print('cancel first');
await Future.delayed(Duration(milliseconds: 20));
secondTimer?.cancel();
print('cancel second');
await Future.delayed(Duration(milliseconds: 20));
thirdTimer?.cancel();
print('cancel third');
}

最佳答案

等待您在计时器回调中完成的 future 。

print('starting');
final completer = Completer();
final t = Timer(Duration(seconds: 3), () => completer.complete());
await completer.future;
print('done');

这样做的问题是,如果取消计时器,完成程序将无法完成,因为回调函数永远不会被调用。所以你必须取消计时器并完成完成器。

Future.delayed 之后检查小部件是否仍然安装不是更容易吗?

编辑

更新您的问题后,您似乎没有遇到上述“问题”。您可以将所有功能封装到一个类中,例如:

class PointlessTimer {
PointlessTimer(Duration d) {
_timer = Timer(d, () => _completer.complete());
}

late final Timer _timer;
final _completer = Completer();

Future get future => _completer.future;

void cancel() {
_timer.cancel();
_completer.complete();
}
}

并像这样使用它:

void main() async {
print('starting');
final pointless = PointlessTimer(Duration(seconds: 3));
Timer(Duration(seconds: 2), () => pointless.cancel());
await pointless.future;
print('done');
}

关于flutter - 使用 Timer 而不是 Future.delayed(...) 创建异步等待步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73170098/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com