如何取消Future.delayed?


问题内容

如何取消/停止Future.delayed?我读了另一个问题:如何取消Future.delayed函数调用,有人回答了这个可能的解决方案 ,但是我不知道如何在代码中使用它,我不知道没有像示例那样的数据列表,我只是不希望在某些情况下执行Future.delayed内部的代码。

await Future.delayed(Duration(seconds: myDuration)).then((_){

  checkAnswer("");
  jumpToNextQuestion();

});

问题答案:

使用Timer

  Timer t = Timer(Duration(seconds: myDuration), () {
    checkAnswer('');
    jumpToNextQuestion();
  });
  // and later, before the timer goes off...
  t.cancel();