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(); });
You can use CancelableOperation or CancelableCompleter to cancel a future.
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.
Use a Timer
.
Timer t = Timer(Duration(seconds: myDuration), () { checkAnswer(''); jumpToNextQuestion(); }); // and later, before the timer goes off... t.cancel();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With