Skip to content

Instantly share code, notes, and snippets.

@lmzach09
Created August 5, 2019 05:12
Show Gist options
  • Save lmzach09/1f6d7101379d7197fb00efadeab20831 to your computer and use it in GitHub Desktop.
Save lmzach09/1f6d7101379d7197fb00efadeab20831 to your computer and use it in GitHub Desktop.
An init function for creating a Dart/Flutter Isolate. It returns the SendPort so the main thread can send messages to the Isolate. It also listens for messages passed from the Isolate.
Future<SendPort> initIsolate() async {
Completer completer = new Completer<SendPort>();
ReceivePort isolateToMainStream = ReceivePort();
isolateToMainStream.listen((data) {
if (data is SendPort) {
SendPort mainToIsolateStream = data;
completer.complete(mainToIsolateStream);
} else {
print('[isolateToMainStream] $data');
}
});
Isolate myIsolateInstance = await Isolate.spawn(myIsolate, isolateToMainStream.sendPort);
return completer.future;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment