Created
August 5, 2019 05:12
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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