How to run background tasks in Flutter using WorkManager

Nanthankumaran S
3 min readOct 3, 2021

--

As you’re searching for background tasks. I assume you’re a bit comfortable with Flutter and the Mobile ecosystem. So I don’t want to bore you with the topics like What is Flutter? and all. So without further redo let’s start the topic.

This article is separated into:

  • Different types of tasks
  • What is WorkManager?
  • How to run background tasks?
  • Some Constraints

Different Types of Tasks

In Mobile Development, there are three kinds of tasks that can run in the background without blocking the user. They are,

  • Foreground — When the application is open, in view & in use.
  • Background — When the application is open, however in the background (minimised). This typically occurs when the user has pressed the “home” button on the device, has switched to another app via the app switcher.
  • Terminated — When the device is locked or the application is not running. The user can terminate an app by “swiping it away” via the app switcher UI on the device

As a developer, we need to take care of all of these different types of tasks for building a User-Friendly Application.

For this handling these different kinds of situations using Flutter, we are going to and package namely WorkManager.

What is WorkManager?

As you all are aware WorkManager is a Flutter Package that can run your dart code in the background. As the package states, WorkManager is,

Flutter WorkManager is a wrapper around Android’s WorkManager and iOS’ performFetchWithCompletionHandler, effectively enabling headless execution of Dart code in the background.

As a first step include WorkManager in your pubspec.yaml.

dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
workmanager: <latest version>

Android Setup:

Android require less or no setup for this package. First, in your AndroidManifest.xml check that if you’re having,

<meta-data
android:name="flutterEmbedding"
android:value="2" />

That’s it you’re good to go.

iOS Setup:

iOS requires more steps than Android. First, in your Podfile, make sure your minimum deployment target is set to 10. platform: ios, '10.0'

Next in your info.plist add,

Then in your project.pbxproj add,

SystemCapabilities = {
com.apple.BackgroundModes = {
enabled = 1;
};
};

Whoof! One last step inside your app’s delegate file add,

Hurray! you did it.

How to run background tasks?

Configuring WorkManager is really easy. In your main.dart, add the following lines of code.

Reason for const fetchBackground = "fetchBackground"; is you can run many background tasks by passing down the unique strings. It will be managed in the switch statement under the callbackDispatcher() function.

In this example, I have included Workmanager().registerPeriodicTask(); which runs periodically for certain intervals. There is something like Workmanager().registerOneOffTask(); which runs only once.

For more information regarding the parameters that we can pass down to WorkManager, you can check out their documentation.

Debug Mode

To know that background tasks are working, you can include debugging option inside Workmanager.initialize();

Workmanager.initialize(
...
isInDebugMode: true,
);

This will give you a notification whenever WorkManager runs.

Screenshot of WorkManager in action

This will be shown whenever your WorkManager runs. In this way, you can get to know whether WorkManager is running.

Some Constraints

  • Whatever Duration which is set under 15 minutes cannot run as you except. Because the minimum possible duration for Android is 15 minutes. If you set durations under 15 minutes it will be automatically overwritten by Android.
  • Sometimes background tasks can even run once per hour even if you set it to 15 minutes. This is not because of WorkManger. This is because OS background restrictions considering the usage of battery effectively.
  • If you are planning for Scheduling Tasks, use android_alarm_manager_plus instead. This leads to another article which will be released soon.
  • Last but an important note. Your manufacturer can be a cause for blocking your background tasks after restart or because of some power-saving settings. To overcome this you need to enable it by your phone’s settings

That’s it for this article. Hope you learn some new skills and can enable background fetching options in your upcoming apps. If you like this article do like, share and comment your thoughts about this article.

--

--

Nanthankumaran S

Associate Engineer at Presidio | Software, Cloud and Data Engineer | 2x AWs