A timer app has one job: be right. In Flutter the easy version is wrong the moment the user locks the phone.
I build Flip Timer, a Pomodoro timer with a flip-clock display. This is how I made it keep correct time when the OS suspends the app. The snippets are trimmed from the real code.
The problem
The obvious design is a Timer.periodic that ticks once a second and adds a second to elapsed . It works while the app is in the foreground. Both iOS and Android suspend the process shortly after the app goes to the background, so the ticks stop. Come back after 10 minutes and a naive timer still says 24:50.
Two things are needed:
When the app comes back, work out how much time really passed.
While the app is gone, still tell the user when the session ends.
Step 1: store the end time, not the elapsed time
When a session starts, save the wall-clock time it should end:
_sessionEndAt = DateTime . now () . add ( state .…