The UI shows two button: One button to start an isolate, and a second button to stop it at next occasion. The UI (widget) code looks like this:
SendPort sendToIsolatePort;
void _onStartIsolateButtonPushed() async {
ReceivePort receivePort = ReceivePort();
receivePort.listen(onMessageReceivedFromIsolate);
Isolate.spawn(runAsIsolate, receivePort.sendPort);
}
void _onStopIsolateButtonPushed() async {
sendToIsolatePort.send("Stop");
}
void onMessageReceivedFromIsolate(var message) {
if (message is String) {
print("Message received from isolate: " + message);
} else if (message is SendPort) {
print("Reply port received");
sendToIsolatePort = message;
sendToIsolatePort.send("Hello World?!?");
}
}
The code in isolate.dart looks like this: (Note: this one's not in a widget or class, just some global functions)
import 'dart:isolate';
SendPort sendPort;
bool isRunning;
void runAsIsolate(SendPort port) async {
sendPort = port;
ReceivePort receivePort = ReceivePort();
receivePort.listen(onIsolateMessageReceived);
isRunning = true;
sendPort.send(receivePort.sendPort);
while (isRunning) {
_doSomething();
_doSomethingMore();
}
receivePort.close();
sendPort.send("Stopped");
print("Leaving isolate...");
}
void onIsolateMessageReceived(var message) {
if (message is String) {
print("Isolate: messate received: " + message);
if (message == "Stop") {
isRunning = false;
}
} else {
print("WTFlutter... " + message.toString());
}
}
void _doSomething() {}
void _doSomethingMore() {}
Now, for some reason, the isolate neither receives the "Hello World?!?" nor the "Stop" message. Do you have any ideas why? And how to fix it?
Also: Is there a simpler (or shorter) way of executing a thread in flutter? The isolate approach, and it's stream communication, appear very much over-complicated for something as common as parallel execution.
Your advise is much appreciated. Thank you.
else
clause and print something, in youronMessageReceivedFromIsolate
to debugsendToIsolatePort
) is delivered throughonMessageReceivedFromIsolate()
). The problem happens when sending from the widget down to the isolate:sendToIsolatePort.send()
seems to be executed (at the widget), butonIsolateMessageReceived()
isn't called (at the isolate). In other words: The messages don't arrive at the isolate. Pretty complicated...while (isRunning)
will consume all time in our main isolate. thus there is no time that youronIsolateMessageReceived
can be called! Try to remove your while loop, and do this instead:while(isRunning) await Future.delayed(Duration(seconds: 1));