Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to schedule background tasks in Flutter?

I have been looking a lot for this but haven't found any packages or a way to schedule background tasks in Flutter. Like in Android there is WorkManager,AlarmManager.

I know I can access those classes using MethodChannel, but I want something that works for iOS and Android both.

(Its very disappointing that a mobile framework doesn't have the ability to schedule background tasks).

like image 397
Gurleen Sethi Avatar asked Aug 06 '18 11:08

Gurleen Sethi


People also ask

How do I create a background service in flutter?

In Flutter, when it comes to implementing background services, we come across certain plugins and packages that ensures us of having our app running in the background, for example Work manager, and other packages that invokes services from native side to execute dart code in background.

How do you use scheduler in flutter?

To use, import package:flutter/scheduler. dart . This library is responsible for scheduler frame callbacks, and tasks at given priorities. The library makes sure that tasks are only run when appropriate.


1 Answers

There is a Medium blogpost that explains how to do this.
However we thought it was way too complicated to set up so it just happens we created a plugin that aids you with this.

//Provide a top level function or static function. //This function will be called by Android and will return the value you provided when you registered the task. //See below void callbackDispatcher() {   Workmanager.executeTask((task) {     print("Native echoed: $task");     return Future.value(true);   }); }  Workmanager.initialize(     callbackDispatcher, //the top level function.     isInDebugMode: true //If enabled it will post a notification whenever the job is running. Handy for debugging jobs ) 

We support Android's Workmanager and iOS performFetch


For now it only works for Android project, but we are looking at iOS soon.
I'll update this answer when it is available.


We have iOS support now too. It is still early alpha, but give a go.
We wrote a complimentary Medium post too.

like image 129
timr Avatar answered Oct 05 '22 16:10

timr