Workmanager class
Make sure you followed the platform setup steps first before trying to register any task.
Android:
- Custom Application class
iOS:
- Enabled the Background Fetch API
Inside your Dart code
Initialize the plugin first
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((taskName, inputData) {
switch(taskName) {
case "":
print("Replace this print statement with your code that should be executed in the background here");
break;
}
return Future.value(true);
});
}
void main() {
Workmanager().initialize(callbackDispatcher);
}
You can schedule a specific iOS task using:
Workmanager().registerOneOffTask()
Please read the documentation on limitations for background processing on iOS.
iOS periodic background fetch task is automatically scheduled if you setup the plugin properly for Background Fetch.
If you are targeting iOS 13+, you can use Workmanager().registerPeriodicTask()
Note: On iOS 13+, adding a BGTaskSchedulerPermittedIdentifiers key to the Info.plist disables the performFetchWithCompletionHandler and setMinimumBackgroundFetchInterval methods, which means you cannot use both old Background Fetch and new registerPeriodicTask at the same time, you have to choose one based on your minimum iOS target version. For details see Using background tasks to update your app
You can schedule Android tasks using:
Workmanager().registerOneOffTask()
orWorkmanager().registerPeriodicTask()
Constructors
- Workmanager.new()
-
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
cancelAll(
) → Future< void> - Cancels all tasks
-
cancelByTag(
String tag) → Future< void> -
Cancels a task by its
tag
-
cancelByUniqueName(
String uniqueName) → Future< void> -
Cancels a task by its
uniqueName
-
executeTask(
BackgroundTaskHandler backgroundTask) → void -
A helper function so you only need to implement a
BackgroundTaskHandler
-
initialize(
Function callbackDispatcher, {bool isInDebugMode = false}) → Future< void> -
This call is required if you wish to use the
WorkManager
plugin.callbackDispatcher
is a top level function which will be invoked by Android or iOS. See the discussion onBackgroundTaskHandler
for details.isInDebugMode
true will post debug notifications with information about when a task should have run -
isScheduledByUniqueName(
String uniqueName) → Future< bool> -
Checks whether a period task is scheduled by its
uniqueName
. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
printScheduledTasks(
) → Future< void> - Prints details of un-executed scheduled tasks to console. To be used during development/debugging.
-
registerOneOffTask(
String uniqueName, String taskName, {String? tag, ExistingWorkPolicy? existingWorkPolicy, Duration initialDelay = Duration.zero, Constraints? constraints, BackoffPolicy? backoffPolicy, Duration backoffPolicyDelay = Duration.zero, OutOfQuotaPolicy? outOfQuotaPolicy, Map< String, dynamic> ? inputData}) → Future<void> - Schedule a one off task.
-
registerPeriodicTask(
String uniqueName, String taskName, {Duration? frequency, Duration? flexInterval, String? tag, ExistingWorkPolicy? existingWorkPolicy, Duration initialDelay = Duration.zero, Constraints? constraints, BackoffPolicy? backoffPolicy, Duration backoffPolicyDelay = Duration.zero, OutOfQuotaPolicy? outOfQuotaPolicy, Map< String, dynamic> ? inputData}) → Future<void> -
Schedules a periodic task that will run every provided
frequency
. -
registerProcessingTask(
String uniqueName, String taskName, {Duration initialDelay = Duration.zero, Constraints? constraints}) → Future< void> - Schedule a background long running task, currently only available on iOS.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- iOSBackgroundProcessingTask → const String
- Use this constant inside your callbackDispatcher to identify when an iOS Background Processing via BGTaskScheduler occurred.
- iOSBackgroundTask → const String
- Use this constant inside your callbackDispatcher to identify when an iOS Background Fetch occurred.