Plugins with dart plugin registrants don't work on background isolates

This issue has been tracked since 2022-09-22.

After #13937, platform channels work on background isolates. Plugins should work as well, but if a plugin relies on a dart plugin registrant (example shared_preferences usage to set the platform_interface subclass) the registrant isn't executed on the background isolate, so the plugin doesn't function correctly. Specifically there aren't handlers registered for the platform channels.

The solution should be to execute the Dart plugin registrant on the background isolate when it registers with the root isolate.

See:

  • PlatformConfigurationNativeApi::RegisterBackgroundIsolate
  • flutter::FindAndInvokeDartPluginRegistrant
gaaclarke wrote this answer on 2022-09-23

fyi @stuartmorgan

I tried the naive solution of calling flutter::FindAndInvokeDartPluginRegistrant in PlatformConfigurationNativeApi::RegisterBackgroundIsolate but oddly crashes. Ideally we can fix this under the covers so BackgroundIsolateBinaryMessenger.ensureInitialized calls the registrant.

stuartmorgan wrote this answer on 2022-09-23

Does calling the Dart plugin ensure-initialized method (I forget the exact name) from the Dart side work?

gaaclarke wrote this answer on 2022-09-23

Does calling the Dart plugin ensure-initialized method (I forget the exact name) from the Dart side work?

Are you talking about calling the generated dart code directly? The generated code is private (class _PluginRegistrant), I wonder if I may have to end up exposing a function that is calling it. I don't know if there already is one. I didn't see one.

I suspect the crash on the c++ solution may be something weird about jumping back into dart after dart calls c++ (ie Dart-> C++-> Dart ).

stuartmorgan wrote this answer on 2022-09-23

I meant calling DartPluginRegistrant.ensureInitialized().

gaaclarke wrote this answer on 2022-09-24

I completely forgot I implemented that, that worked out.

More Details About Repo
Owner Name flutter
Repo Name flutter
Full Name flutter/flutter
Language Dart
Created Date 2015-03-06
Updated Date 2022-09-30
Star Count 145381
Watcher Count 3565
Fork Count 23363
Issue Count 11208

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date