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() or Workmanager#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 on BackgroundTaskHandler 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. The taskName is the value that will be returned in the BackgroundTaskHandler The inputData 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. A uniqueName is required so only one task can be registered. The taskName is the value that will be returned in the BackgroundTaskHandler a frequency is not required and will be defaulted to 15 minutes if not provided. a frequency has a minimum of 15 min. Android will automatically change your frequency to 15 min if you have configured a lower frequency. The inputData 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.