Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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();

});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.8k views
Welcome To Ask or Share your Answers For Others

1 Answer

Use a Timer.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...