5

I'm building a Flutter app that will have the capability to execute some actions when the device connects to another bluetooth device. This app should work on Android and iOS but for the sake of simplicity I'll focus on Android in this post. Also, this has to work whether the app is in the foreground, in the background or killed.

Here is the architecture of the app:

  1. I have an Android native code that registers to bluetooth events through a BroadcastReceiver.
  2. I followed this tutorial to set up the communication between the Android code and the Flutter code: https://medium.com/@chetan882777/initiating-calls-to-dart-from-the-native-side-in-the-background-with-flutter-plugin-7d46aed32c47.
  3. When the Android BroadcastReceiver is triggered by a bluetooth event, the information is sent to the Flutter code (even if the app was in the background or killed). A Flutter isolate is created to handle the Flutter code.

Everything works perfectly well. The Flutter code is called and I can use print(data) to log the data that have been provided by the Android code.

Things are becoming more tricky when, from the isolate, I want to call any Flutter plugin (like sqflite, package_info_plus, ...). I get this error every time:

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: MissingPluginException(No implementation found for method xxx on channel yyy)

I understand that spawned isolate can't natively run Flutter plugins. There are some posts (Unable to understand Flutter Isolate workaround for "'Window_sendPlatformMessage' (4 arguments) cannot be found" error, https://github.com/flutter/flutter/issues/13937) that explain how to create isolates that can run Flutter plugins by using a workaround or a plugin like https://pub.dev/packages/flutter_isolate. However, I can't create the isolate with this package because the isolate is created from the Android code.

Can one of you tell me how I can achieve this? Is there a way to use Flutter plugins from an isolate that has been created by native code?

Thank you very much in advance

Valentin
  • 5,379
  • 7
  • 36
  • 50
  • Does my answer solve your question? If so, you can accept the answer and award the bounty – ch271828n Oct 19 '21 at 01:42
  • I have a similar problem. could you find any solution ? https://stackoverflow.com/questions/69522573/missingpluginexception-when-using-a-plugins-with-alarm-manager-call-back – Sajjad Oct 19 '21 at 05:31

1 Answers1

0

Sounds like the way you create the Flutter isolate may not be compatible with your goal. There are some solutions to integrate Flutter with native Android/iOS projects, such as flutter_boost (disclaimer: I have not tried it and not sure good or not; you may find many other alternatives as well, this is just an example). You can use that to create the Flutter environments. Since the solutions above allow some Android code to open a new normal Flutter page and Flutter code in that page is able to do anything (of course include calling native - otherwise things like flutter_boost is really useless), this should work.

ch271828n
  • 15,854
  • 5
  • 53
  • 88