skip to Main Content

I’m having trouble passing and retrieving inputData in Flutter’s Workmanager. I’m trying to send some data (like id and date) when registering a task, but when I try to access the inputData in the background task, I keep getting null.

Workmanager().registerOneOffTask(
  "simpleTask", // Unique task name
  "simpleTask", // The task name
  tag: '1',
  inputData: {'id': '1', 'date': '16-10-2024'}, // Input data to be passed
);

void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {
    print('Task name: $task');
    print('Input data: $inputData'); // This prints null
    print('Notification id: ${inputData?["id"]}');
    print('Notification date: ${inputData?["date"]}');
    return Future.value(true);
  });
}

Workmanager().initialize(
  callbackDispatcher,
  isInDebugMode: true // For debugging
);

Logs when Workmanager().executeTask call

2

Answers


  1. Chosen as BEST ANSWER

    The issue you're encountering is related to the version of the workmanager package. Instead of using the following dependency:

    workmanager: ^0.5.2
    

    You should reference a specific commit from the GitHub repository to resolve this issue. Update your pubspec.yaml file like so:

    workmanager:
      git:
        url: https://github.com/fluttercommunity/flutter_workmanager.git
        ref: b783000
    

    This points to a specific commit that addresses the problem you're facing.

    It's likely that the Flutter Workmanager team will resolve this in future releases, so keep an eye out for updates. Once the issue is officially fixed in a newer release, you should be able to switch back to using the standard versioning.


  2. Make sure you initialize Workmanager before registering any tasks, and ensure your callbackDispatcher is a top-level function annotated with @pragma('vm:entry-point'). Here’s how you can adjust your code:

    1. Initialize Workmanager before registering the task:

      Workmanager().initialize(
        callbackDispatcher,
        isInDebugMode: true, // For debugging
      );
      
      Workmanager().registerOneOffTask(
        "simpleTask", // Unique task name
        "simpleTask", // The task name
        tag: '1',
        inputData: {'id': '1', 'date': '16-10-2024'}, // Input data to be passed
      );
      
    2. Annotate your callbackDispatcher with @pragma('vm:entry-point') and ensure it’s a top-level function:

      @pragma('vm:entry-point')
      void callbackDispatcher() {
        Workmanager().executeTask((task, inputData) {
          print('Task name: $task');
          print('Input data: $inputData'); // This should now print the inputData
          print('Notification id: ${inputData?["id"]}');
          print('Notification date: ${inputData?["date"]}');
          return Future.value(true);
        });
      }
      

    By initializing Workmanager before registering the task and properly annotating the callbackDispatcher, you should be able to access the inputData in your background task.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search