Stop how to stop the timer in flutter?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Stop how to stop the timer in flutter?



I have created a timer in flutter and everything works fine. Now I cant figure out how to dismiss the timer after I started it.
The docs say that you can cancel it by calling "void cancel()" but I don't understand the implementation.

How am I supposed to call it?
And is it the right approach?


static const timeout = const Duration(seconds: 5);
static const ms = const Duration(milliseconds: 1);

startTimeout([int milliseconds]) {
var duration = milliseconds == null ? timeout : ms * milliseconds;
return new Timer(duration, handleTimeout);
}

void handleTimeout() { // callback function
Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(
builder: (BuildContext context) => new ScorePage(quiz.score, quiz.length)),(Route route) => route == null);
return;
}




1 Answer
1



Just keep a reference to the time and cancel it when you don't need it anymore


var timer = startTimeout(100);
timer.cancel();






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived