如何取消Future.delayed? [英] How do I cancel Future.delayed?

查看:428
本文介绍了如何取消Future.delayed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I cancel/stop a Future.delayed? I read this other question: how can i cancel Future.delayed function calling and someone answered with this possible solution: https://dart.academy/how_cancel_future/, but I don't know how to use it in my code, I don't have a List of Data like the example, I just don't want the code inside the Future.delayed be executed in certain case.

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

  checkAnswer("");
  jumpToNextQuestion();

});


推荐答案

使用计时器

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

这篇关于如何取消Future.delayed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆