flutter_workmanager icon indicating copy to clipboard operation
flutter_workmanager copied to clipboard

Context not available

Open alexbrrsclnt opened this issue 6 years ago • 2 comments

Context is not available to use in tasks launched from callbackDispatcher if this method is placed as top-level. I need context to use an inherited widget to recover app's config before make a http request, but context is not available from a top-level method.

I have tried to initialize workmanager inside MaterialApp widget and change the top-level callbackDispatcher method shown in the example with a static function

Workmanager.initialize(
      BackgroundTaskService.callbackDispatcher,
      isInDebugMode: true,
    );
class BackgroundTaskService {

  static void callbackDispatcher() {
    
    Workmanager.executeTask((task, inputData) {
      try{
        print('-----------> Native called background task: $task');
        switch (task) {
          case PERIODIC_TASK:
          case Workmanager.iOSBackgroundTask:
            print('###### The iOS background fetch was triggered ######');
           getConfiguration(updateState: true); // http request
            break;
        }
      } catch (_) {
        print('callbackDispatcher ERROR: ${_.toString()}');
      }

      return Future.value(true);
    });
  }
}

but I get this error:

E/flutter (32109): [ERROR:flutter/shell/common/shell.cc(213)] Dart Error: Unhandled exception:
E/flutter (32109): NoSuchMethodError: No top-level getter 'callbackDispatcher' declared.
E/flutter (32109): Receiver: top-level
E/flutter (32109): Tried calling: callbackDispatcher
E/flutter (32109): #0      NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:207:5)
E/flutter (32109): [ERROR:flutter/runtime/dart_isolate.cc(464)] Could not resolve main entrypoint function.
E/flutter (32109): [ERROR:flutter/shell/common/engine.cc(214)] Could not run the isolate.
E/flutter (32109): [ERROR:flutter/shell/common/engine.cc(139)] Engine not prepare and launch isolate.
E/flutter (32109): [ERROR:flutter/shell/common/shell.cc(485)] Could not launch engine with configuration.

and the task is not executed.

I have also tried to declare the top level callbackDispatcher and from here, call my static method, but this also fails in get the context available.

Any suggestion?

alexbrrsclnt avatar Jul 03 '20 09:07 alexbrrsclnt

You can not use context in a background task, as it runs in its own isolate (https://api.dart.dev/stable/2.8.4/dart-isolate/dart-isolate-library.html). The UI "thread" is a separate isolate.

Isolates can only communicate via messages (they don't have shared memory, therefore context is not available), but unfortunately I haven't done something similar yet, so I can not provide an example.

I also had problems moving the callbackDispatcher inside a class, declaring it as a static function. It only worked for me declaring as a top level function.

gebsl avatar Jul 03 '20 10:07 gebsl

@alexbrrsclnt: Just found a comment on an issue that might be helpful for you -> https://github.com/vrtdev/flutter_workmanager/issues/151#issuecomment-612777877

Apparently describes how to set up a channel between two isolates.

gebsl avatar Jul 10 '20 08:07 gebsl