Since my comments get no reply from the asker, I provide a more detailed proposal here. Feel free to comment and I will update based on the feedback.
Firstly, try to simply follow the tutorial: https://medium.com/flutter/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124
Secondly, the IsolateNameServer seems promising. Looking at its implementation, we see:
class IsolateNameServer {
...
static SendPort? lookupPortByName(String name) {
...
return _lookupPortByName(name);
}
...
static SendPort? _lookupPortByName(String name)
native 'IsolateNameServerNatives_LookupPortByName';
static bool _registerPortWithName(SendPort port, String name)
native 'IsolateNameServerNatives_RegisterPortWithName';
static bool _removePortNameMapping(String name)
native 'IsolateNameServerNatives_RemovePortNameMapping'
}
and the c++ code: https://blog.weghos.com/flutter/engine/flutter/lib/ui/isolate_name_server/isolate_name_server_natives.cc.html
...
Dart_Handle IsolateNameServerNatives::LookupPortByName(
const std::string& name) {
auto name_server = UIDartState::Current()->GetIsolateNameServer();
if (!name_server) {
return Dart_Null();
}
Dart_Port port = name_server->LookupIsolatePortByName(name);
if (port == ILLEGAL_PORT) {
return Dart_Null();
}
return Dart_NewSendPort(port);
}
...
Therefore, it is not implemented in Dart, but implemented natively, so probably the background isolate can register itself and the main isolate can use this to find it.
Try it and if it does not work, please comment and I will update.