Flutter WorkManager is a wrapper around Android's WorkManager and iOS' performFetchWithCompletionHandler, effectively enabling headless execution of Dart code in the background.
This is especially useful to run periodic tasks, such as fetching remote data on a regular basis.
This plugin was featured in this Medium blogpost
dependencies:
workmanager: ^0.0.15
flutter pub get
import 'package:workmanager/workmanager.dart';
In order for background work to be scheduled correctly you should follow the Android and iOS setup first.
See sample folder for a complete working example.
Before registering any task, the WorkManager plugin must be initialized.
void callbackDispatcher() {
Workmanager.executeTask((backgroundTask) {
print("Native called background task: $backgroundTask"); //simpleTask will be emitted here.
return Future.value(true);
});
}
void main() {
Workmanager.initialize(
callbackDispatcher, // The top level function, aka callbackDispatcher
isInDebugMode: true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
);
Workmanager.registerOneOffTask("1", "simpleTask");
runApp(MyApp());
}
The
callbackDispatcher
needs to be either a static function or a top level function to be accessible as a Flutter entry point.
Not every Android WorkManager
feature is ported.
Two kinds of background tasks can be registered :
- One off task : runs only once
- Periodic tasks : runs indefinitely on a regular basis
// One off task registration
Workmanager.registerOneOffTask(
"1",
"simpleTask"
);
// Periodic task registration
Workmanager.registerPeriodicTask(
"2",
"simplePeriodicTask", \
// When no frequency is provided the default 15 minutes is set.
// Minimum frequency is 15 min. Android will automatically change your frequency to 15 min if you have configured a lower frequency.
frequency: Duration(hours: 1),
)
Each task must have an unique name;
This allows cancellation of a started task.
The second parameter is the String
that will be send to your callbackDispatcher
function, indicating the task's type.
You can set the optional tag
property.
Handy for cancellation by tag
.
This is different from the unique name in that you can group multiple tasks under one tag.
Workmanager.registerOneOffTask("1", "simpleTask", tag: "tag");
Indicates the desired behaviour when the same task is scheduled more than once.
The default is KEEP
Workmanager.registerOneOffTask("1", "simpleTask", existingWorkPolicy: ExistingWorkPolicy.append);
Indicates how along a task should waitbefore its first run.
Workmanager.registerOneOffTask("1", "simpleTask", initialDelay: Duration(seconds: 10));
Not all constraints are mapped.
Workmanager.registerOneOffTask(
"1",
"simpleTask",
constraints: Constraints(
networkType: NetworkType.connected,
requiresBatteryNotLow: true,
requiresCharging: true,
requiresDeviceIdle: true,
requiresStorageNotLow: true
)
);
Indicates the waiting strategy upon task failure.
The default is BackoffPolicy.exponential
.
You can also specify the delay.
Workmanager.registerOneOffTask("1", "simpleTask", backoffPolicy: BackoffPolicy.exponential, backoffPolicyDelay: Duration(seconds: 10));
A task can be cancelled in different ways :
Cancels the task that was previously registered using this Tag, if any.
Workmanager.cancelByTag("tag");
Workmanager.cancelByUniqueName("<MyTask>");
Workmanager.cancelAll();