My flutter app allows users to time how long they have been working for. I'm using the package stop_watch_timer.
Everything works fine except when the app gets killed in the background, because then I lose the timer value and it goes back to 0. As users are likely to start the timer then put their phone away and work for a while, the app getting killed is a very likely scenario and I need to make sure that this never happens. Another feature that I need in the app is for users to be able to pause and unpause the timer whenever they want.
Here are the solutions I have come up with so far and their problems:
Solution 1: Store the start
DateTimeon the phone usingshared_preferencesand calculate the current timer value when app is reopened by simply calculating the duration between start time andDateTime.now(). But then I realised that this does not work if the user had paused and unpaused the timer at some point.Solution 2: Store the current timer value as a
RestorableInt. This works if the timer is on pause when the app gets killed, but does not work if the timer is meant to be running when the app dies.
Any suggestions or ideas??