androidflutterdart-isolates

Background isolate platform channel difference between internal and external plugins


I have a background isolate trying to use a platform channel. Yes, Flutter 3.7+, channels set up according to the background isolate requirements (including calling BackgroundIsolateBinaryMessenger.ensureInitialized()). From the isolate, I can successfully call outside plugins communicating through their channels, everything works beautifully. The problem is when calling my own plugin inside the same project. And I cannot identify what's handled differently in the framework depending on where the plugin is located.

Being an internal plugin, the toolchain doesn't pick it up and doesn't add it to GeneratedPluginRegistrant, so I do it manually in my FlutterActivity:

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
  flutterEngine.plugins.add(MainPlugin())
  super.configureFlutterEngine(flutterEngine)
}

My plugin is a standard FlutterPlugin, also ActivityAware and MethodCallHandler. I tried to set up the channel both in the simpler form and on the task queue as the documentation recommends:

override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
  val taskQueue = binding.binaryMessenger.makeBackgroundTaskQueue()
  channel = MethodChannel(binding.binaryMessenger, "com.example.foreground_test/platform", StandardMethodCodec.INSTANCE, taskQueue)
  //channel = MethodChannel(binding.binaryMessenger, "com.example.foreground_test/platform")
  channel.setMethodCallHandler(this)
}

The plugin works from the main isolate all right, just not from the background one. The latter results in the usual MissingPluginException(No implementation found for method test on channel com.example.foreground_test/platform) error.

What might be set up differently by the framework when the plugin is inside? Why does it matter at all in the first place?


Solution

  • After much investigation, with input from the core Flutter developers, the cause was identified. The code created a secondary FlutterEngine and while outside plugins were automatically associated with both, my own internal code was only registered to the main engine manually, not to the secondary one.