Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i cancel Future.delayed function calling

Tags:

flutter

dart

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
  });
});
like image 781
Deepak Gehlot Avatar asked Oct 17 '22 08:10

Deepak Gehlot


1 Answers

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

like image 168
Dev Avatar answered Oct 21 '22 00:10

Dev