Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run clock timer in background on flutter?

I am try to create a timer app which have multiple countdown timer for different task. Issue, i am facing is that, if i start one timer, and press back button, timer stops. So i want, that timer to run till either it is being paused or timer ends and alerts the user or app is destroyed.Help me how can i do this using Flutter? Also i using sql-lite to store all timers.

like image 506
Tushar Asodariya Avatar asked Dec 25 '19 16:12

Tushar Asodariya


2 Answers

I had a similar problem. The isolate won't solve it since the isolates life cycle is also linked to the main isolate(ui thread). I sort of created a workaround,i.e I used the dateTime property,so if the user chooses 30 minutes,the final Date time becomes Date time.now(Duration (minutes:30));. And a timer periodic function which runs every minute and finds the difference between the final date time and the datetime.now() so even if the os kills ur app while in the background application tray when u reopen the app,the time automatically updates to the change. I also used flutter notification package to alert the user when the timer value becomes zero

like image 144
lone monk Avatar answered Oct 25 '22 15:10

lone monk


Isolates won't solve the problem. even isolates gets paused by the OS when the app is in the background. I have tried it (in Android10 timer runs for about 100 seconds before it gets paused by the system).

for your problem Android_Alarm_Manager package seems to be the solution. but it only supports android. no ios support yet

like image 40
Ali Muhammad Ali Avatar answered Oct 25 '22 13:10

Ali Muhammad Ali