How to call a Flutter function from native implementation of Work manager in android?

1.2k Views Asked by At

Have a look at the images. I'm trying to implement the android work manager natively via the method channel. Now, if we start the work manager, I need to call a method on the Flutter side.

I can't find a way to do that. I need flutterEngine for that, and that object should come from FlutterActivity. But there is no way to pass that to the work manager. It lets you pass only data types like int, bool, double, and string.

Flutter Activity: enter image description here

Worker: enter image description here

1

There are 1 best solutions below

0
On

You can use the package or implement it similarly with the package https://pub.dev/packages/workmanager

On flutter

  • write the top level function
  • when initialing app, get callback handle key with PluginUtilities.getCallbackFromHandle(Function callback)
  • send the callback handle key to Android

On Android

  • save the callback handle key with SharedPreferences
  • when you need, create FlutterEngine and run dart code(engine.dartExecutor.executeDartCallback) using the saved callback handle key