簡體   English   中英

使用 Timer 而不是 Future.delayed(...) 創建異步等待步驟

[英]create async await steps with Timer instead of Future.delayed(...)

是否可以使用Timer而不是Future.delayed執行異步步驟? 主要原因是Timer class 中的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');
}

使用 Richard Heap 回答后的計時器,我想盡可能取消這些步驟,但下面的代碼會提示:

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');
}

await您在計時器回調中complete的未來。

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

問題是如果定時器被取消,完成者將無法完成,因為回調永遠不會被調用。 所以你必須取消計時器並完成完成者。

Future.delayed之后檢查小部件是否仍然安裝會更容易嗎?

編輯

在更新您的問題之后,您似乎沒有遇到上述“問題”。 您可以將所有功能封裝到 class 中,例如:

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');
}

遵循 Richard Heap 的回答:

/// works as a Future.delayed. Returns a timer to be canceled.
/// useful when disposing widgets before a timer completion.
/// necessary when testing widgets that disposes before the timer completion.
  Future<Timer> _delayed(Duration duration) async {
    final completer = Completer();
    final timer = Timer(duration, () {
      completer.complete();
    });
    await completer.future;
    return timer;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM