I have a code like below:
Timer(Duration(seconds: 5),(){
print("This is printed after 5 seconds.");
});
print("This is printed when Timer ends");
How can I use "await" in this case? I want when Timer ends then run next code below Timer. I used Future.delayed(), it can do it but I can't skip the delay time in Future.delayed() like in Timer(). Because i want to skip the delay time if the condition is true. Because I want to skip the delay time if the condition is true. If I use Future.delayed(), it doesn't have cancel() method like Timer(). Please tell me the solution. Thanks
CodePudding user response:
Try Future.delayed instead of Timer
await Future.delayed(Duration(seconds: 5),(){
print("This is printed after 5 seconds.");
});
print('This is printed when Timer ends');
CodePudding user response:
Timer(Duration(seconds: 5),(){
print("This is printed after 5 seconds.");
printWhenTimerEnds();
});
void printWhenTimerEnds(){
print("This is printed when Timer ends");
}
When you wish to skip the timer just call timer cancel and the printWhenTimerEnds() method