flutter_background_service icon indicating copy to clipboard operation
flutter_background_service copied to clipboard

Documentation on how to pass receive and send ports to the background isolate

Open RickPoleshuck opened this issue 1 year ago • 4 comments

I need to communicate between the UI and the background service. For a normal isolate, I would do this by passing sendport and receiveport to the isolate. Can I do this at all with flutter_background_service?

RickPoleshuck avatar Jul 17 '23 17:07 RickPoleshuck

I'm afraid you can't do that. Normal isolate spawned by main isolate, but in this plugin the isolate created by BackgroundService class, on the other words we have 2 main isolates there.

ekasetiawans avatar Aug 22 '23 16:08 ekasetiawans

I'm afraid you can't do that. Normal isolate spawned by main isolate, but in this plugin the isolate created by BackgroundService class, on the other words we have 2 main isolates there.

I have switched to using SQLite for my preferences and that works fine. Thanks for the response.

RickPoleshuck avatar Aug 22 '23 16:08 RickPoleshuck

I've been dealing with the same problem while working on android_alarm_manager_plus where the same logic applies (isolate created by the lib). In their example code there is a declaration of ReceivePort and SendPort that are used to communicate beween isolates so check it out and maybe it will help you with your issue.

From my testing it's required to have ReceivePort as global variable, also as mentioned in SendPort documentation there are limits to what can be sent this way so keep that in mind. Some of the types that should work didn't work for me though, I'm currently sending objects as List<String> converted from json and it's semi-working.

xmooncake avatar Oct 12 '23 13:10 xmooncake

I'm afraid you can't do that. Normal isolate spawned by main isolate, but in this plugin the isolate created by BackgroundService class, on the other words we have 2 main isolates there.

Is it possible for the intermediate isolate to forward / proxy messages via SendPort and ReceivePort from the main and third isolates?

I see in https://github.com/ekasetiawans/flutter_background_service/issues/148#issuecomment-1113710863 that you mention an inability to send arbitrary objects via SendPort, according to https://api.flutter.dev/flutter/dart-isolate/SendPort/send.html. I'm surprised by that documentation, because with a regular isolate, I am able to send instances of classes like the following:

class IsolateMessage {
  const IsolateMessage(
      {required this.name,
      this.id,
      this.alerts,
      this.sourceStrings,
      this.sources,
      this.forceRefreshNow});
  final String name;
  final int? id;
  final List<Alert>? alerts;
  final List<String>? sourceStrings;
  final List<AlertSource>? sources;
  final bool? forceRefreshNow;
}

That's when using the following example code as a starting point: https://dart.dev/language/isolates#complete-example.

Would adding SendPort and ReceivePort be possible, and something you want to support in your plugin?

leaf-node avatar Jul 10 '24 18:07 leaf-node