flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
166.17k stars 27.46k forks source link

Use MethodChannel from an Isolate without plugin #126085

Closed razfazz closed 1 year ago

razfazz commented 1 year ago

Is there an existing issue for this?

Use case

In some cases creating a plugin (Just to work around the MethodChannel background isolate limitations) not only causes unnecessary boilerplate code but also causes some hurdles. For example when including native .aar files.

In my particular use case, an external native Android Foreground service is started. The code for this is in an .aar file that is included.

Starting the service from the Flutter UI Isolate by using MethodChannel is very simple and straightforward.

However, calling the MethodChannel from a non UI Isolate requires the creation of a plugin, according to the documentation, which complicates the whole process, especially when including .aar files.

Proposal

Make it possible to overwrite the onAttachedToEngine using the taskQueue api without being forced to write a plugin

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
  val taskQueue =
      flutterPluginBinding.binaryMessenger.makeBackgroundTaskQueue()
  channel = MethodChannel(flutterPluginBinding.binaryMessenger,
                          "com.example.foo",
                          StandardMethodCodec.INSTANCE,
                          taskQueue)
  channel.setMethodCallHandler(this)
}
stuartmorgan commented 1 year ago

Just to work around the MethodChannel background isolate limitations

What limitations are you referring to exactly?

However, calling the MethodChannel from a non UI Isolate requires the creation of a plugin, according to the documentation

Which documentation says that? There is nothing plugin-specific about platform channels.

Make it possible to overwrite the onAttachedToEngine using the taskQueue api

I'm not at all sure what this means. You're also showing Java-side code, but the rest of the issue is about Dart isolates; those are completely orthogonal.

Please provide the Dart code you are trying to use for your isolate and specific details about what didn't work.

github-actions[bot] commented 1 year ago

Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. If you find this problem please file a new issue with the same description, what happens, logs and the output of 'flutter doctor -v'. All system setups can be slightly different so it's always better to open new issues and reference the related ones. Thanks for your contribution.

github-actions[bot] commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.