How to implement Timer reliably (persistent)

72 views
Skip to first unread message

Yu Shen

unread,
Feb 29, 2020, 8:15:14 PM2/29/20
to Flutter Development (flutter-dev)
In my application, 
It needs a timer to asynchronously launch a new UI screen when the timer expires. 

The following code shows the desired semantics:

 Timer t = Timer(Duration(seconds: myDuration), () {
    askQuestion('How are you doing?');
  });
  // Maybe later, before the timer goes off...
  t.cancel();
Using Timer, my implementation works sometimes, especially with small durations. But when the duration is large, say 25, or 400 minutes, then my app would be killed and restarted, then the Timer is lost, the timeout will never happen.

My question is how I can make the Timer persistent surviving the restart of the app or some mechanism to have reliable Timer behavior? 
Thanks!
Yu (Aaron)

J'ecrite, donc je pense. 
Je pense, donc je suis.

Suzuki Tomohiro

unread,
Feb 29, 2020, 11:12:23 PM2/29/20
to Yu Shen, Flutter Development (flutter-dev)
I would save both timer start time and its duration in a file (or somewhere). When your app starts up, it reads them to check whether to resume timer or not.


--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/CAF5VkCWU2fq9U2ZiFaW2cYtJhmVovbTtfozznHjNUJkT_oM4cg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages