Flutter - timer bugs out when the screen is locked

413 Views Asked by At

I'm trying to create countdown timer where I keep track off the session when it starts. I'm using the timer when the app is in background it runs fine but when the app is in background and the screen is locked the timer does not work properly.

I know a method where when the app is paused stops the timer & save the time and when is resumed the calculate the time according. But if the user destroys the app when it's in background then what should I do since I'm not good in native development(still learning native development).

please help me. Thank you.

1

There are 1 best solutions below

0
On

Couldn't you save the current time (and presumably total duration) to storage?

That is, when the timer starts, write something to SharedPreferences that records the current time and total duration (countdown time). While the app is in foreground / background, use the code you currently have. If the user destroys the app and restarts, the application can check the saved values on start. It can then calculate whether the duration has already passed, and handle accordingly.

This might also handle the issue you mentioned about timer not working properly when app is in background and the screen is locked - which makes sense, because I don't think the application is necessarily running at that point.