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.
You can now schedule Android tasks using:
Workmanager#registerOneOffTask()
orWorkmanager#registerPeriodicTask
iOS periodic task is automatically scheduled if you setup the plugin properly.
Constructors
- Workmanager()
-
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 -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
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
A
uniqueName
is required so only one task can be registered. ThetaskName
is the value that will be returned in theBackgroundTaskHandler
TheinputData
is the input data for task. Valid value types are: int, bool, double, String and their list -
registerPeriodicTask(
String uniqueName, String taskName, {Duration? frequency, 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
. AuniqueName
is required so only one task can be registered. ThetaskName
is the value that will be returned in theBackgroundTaskHandler
afrequency
is not required and will be defaulted to 15 minutes if not provided. afrequency
has a minimum of 15 min. Android will automatically change your frequency to 15 min if you have configured a lower frequency. TheinputData
is the input data for task. Valid value types are: int, bool, double, String and their list -
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.