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

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

dart - How do I cancel 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();

});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use a Timer.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...