A simple timer that can be scheduled to run at a specific time.
Please note: This timer only runs when the app is running - it does not schedule anything to be run in the background using OS-specific scheduling mechanisms, nor does it attempt to wake the device or app.
A ScheduledTimer
can be scheduled to run at a specific time, rather than after a certain amount of time (which is the case with the normal Flutter Timer
).
The scheduled time is automatically stored (using SharedPreferences
) and retrieved after an app restart.
If a scheduled time was missed (i.e. the app was not running at the time), ScheduledTimer
can optionally call a user defined callback function. This makes it possible to reschedule or execute the timer immediately.
ScheduledTimer myTimer;
myTimer = ScheduledTimer(
id: 'myTimer',
onExecute: (){
print('Executing myTimer');
},
defaultScheduledTime: DateTime.now().add(Duration(minutes: 10)),
onMissedSchedule: (){
print('myTimer missed the scheduled time');
myTimer.execute(); // Execute onExecute() immediately
}
);