Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cancel Future.delayed?

Tags:

flutter

dart

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();  }); 
like image 335
djalmafreestyler Avatar asked May 13 '19 21:05

djalmafreestyler


People also ask

How do you cancel a Future in darts?

You can use CancelableOperation or CancelableCompleter to cancel a future.

How do you use the Future in darts?

A future (lower case “f”) is an instance of the Future (capitalized “F”) class. A future represents the result of an asynchronous operation, and can have two states: uncompleted or completed. Note: Uncompleted is a Dart term referring to the state of a future before it has produced a value.


1 Answers

Use a Timer.

  Timer t = Timer(Duration(seconds: myDuration), () {     checkAnswer('');     jumpToNextQuestion();   });   // and later, before the timer goes off...   t.cancel(); 
like image 56
Richard Heap Avatar answered Oct 14 '22 17:10

Richard Heap