Flutter Workmanager Task not updating with app update

2.1k Views Asked by At

So I have an issue where my existing Flutter Workmanager background task is not updating automatically following the app's update.

I have the example code which sets up a periodic Workmanager job.

void main() async
{
  WidgetsFlutterBinding.ensureInitialized();
  
  Workmanager().initialize(
      callbackDispatcher
    );

  await Workmanager().registerPeriodicTask(
      "ExampleTask",
      "ExampleTaskPeriodicTask",
      initialDelay: Duration(minutes: 15,
      frequency: Duration(hours: 1)
    );
}

void callbackDispatcher() async
{
  Workmanager().executeTask((task, inputData) async 
  {
    await DoSomething();
    
    return Future.value(true);
  }); 
}

If I update DoSomething() in my background task, currently it does not update for the user and it would continue doing the old thing, not the new thing.

Is there any way to update the background task without the user having to do anything following the apps automatic update?

2

There are 2 best solutions below

0
On BEST ANSWER

Through testing and debugging using adb to update the app (flutter run is not suitable), the resolution was to add existingWorkPolicy: ExistingWorkPolicy.append to the periodic task registry.

0
On
  Workmanager().initialize(callbackDispatcher);
  Workmanager().registerPeriodicTask(
    'uniqueTaskName', // Unique name for the task
    'simplePeriodicTask',
    frequency: Duration(seconds: 15),
    // Repeat interval in seconds (15 seconds in this case)
    existingWorkPolicy:
        **ExistingWorkPolicy.append,** // Append new tasks to existing ones
  );