How to schedule background tasks in Flutter?
//Provide a top level function or static function.
//This function will be called by Android and will return the value you provided when you registered the task.
//See below
void callbackDispatcher() {
Workmanager.executeTask((task) {
print("Native echoed: $task");
return Future.value(true);
});
}
Workmanager.initialize(
callbackDispatcher, //the top level function.
isInDebugMode: true //If enabled it will post a notification whenever the job is running. Handy for debugging jobs
)
Use WorkManager
library
dependencies:
workmanager: ^0.2.3
WorkManager
comes under two parts, which run the task in the background.
1. Delayed background work
registerOneOffTask runs the task only once with an initial delay of 10 seconds. This is useful when we need to perform any background work only once.
Example:
Workmanager.registerOneOffTask(
"1",
"registerOneOffTask",
initialDelay: Duration(seconds: 10),
);
Copyright © 2021 Dtuto.Com