Reputation: 2961
how can i cancel Future.delayed
i am using Future.delayed for some task, but if i want to cancel this delayed task, so is their any method or any other things to use.
Future.delayed(Duration(seconds: 10),(){
setState(() {
//some method calling
});
});
Upvotes: 5
Views: 4286
Reputation: 6776
what about declare a bool value
bool _executeFuture=true;
then
Future.delayed(Duration(seconds: 10),(){
if(_executeFuture){
setState(() {
//some method calling
});
}
});
Now whenever you want to cancel Future just use
_executeFuture=false;
Also, You can use CancelableOperation from https://pub.dartlang.org/packages/async
Upvotes: 2