fluttercommunity / flutter_workmanager

A Flutter plugin which allows you to execute code in the background on Android and iOS.
851 stars 260 forks source link

I just ran example app but causing error The `callbackDispatcher` is a top level function. #491

Closed kotran88 closed 1 year ago

kotran88 commented 1 year ago

what may be the problem ? android setup page doesn't mention any about it. flutter 3.10

I just push Start the Flutter background service also Register Perodic Task(Android)

whole error log says


E/flutter (25555): 
E/flutter (25555): `Workmanager().initialize(
E/flutter (25555):   callbackDispatcher,
E/flutter (25555):  )`
E/flutter (25555): 
E/flutter (25555): The `callbackDispatcher` is a top level function. See example in repository., null, null)
E/flutter (25555): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7)
E/flutter (25555): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310:18)
E/flutter (25555): <asynchronous suspension>
E/flutter (25555): #2      Workmanager.registerPeriodicTask (package:workmanager/src/workmanager.dart:228:7)
E/flutter (25555): <asynchronous suspension>
makis73 commented 1 year ago

I had to add this in the main():

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Workmanager().initialize(
      callbackDispatcher, // The top level function, aka callbackDispatcher
      isInDebugMode:
          true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
      );
  runApp(MyApp());
}
ened commented 1 year ago

Yes, thank you for following the docs. :)