I have an isolate that works as expected except that execution never progresses beyond the for loop marked <--HERE
. I am just learning Isolates, but it seems that even though Isolate.exit(sp,0)
has been called, the isolate does not complete. After printing out the numbers, 'done' is never printed and the program just waits for nothing indefinitely.
How can I work this so that the program continues execution once the last number has been sent to the ReceviePort
?
import 'dart:convert';
import 'dart:isolate';
main() async {
await for(final n in getNumbers()){
print(n);
} // <--HERE
print('done');
}
Stream<int> getNumbers() {
final rp = ReceivePort();
Isolate.spawn(_getNumbers, rp.sendPort);
return rp.cast();
}
void _getNumbers(SendPort sp) async {
// ... get json from server ...
var jsonString = '[1,2,3,47,42,9]'; // an arbitrary amount of numbers
var li = jsonDecode(jsonString);
for (final num in li) {
sp.send(num);
}
Isolate.exit(sp,0);
}
UPDATE:
I was able to achieve the desired behaviour by using a second receiveport for the onExit callback. But it does not feel like the cleanest solution. If anyone has a better way, please share. Here's what I'm at now (as above except getNumbers):
Stream<int> getNumbers() {
final rp = ReceivePort();
final rp2 = ReceivePort()..first.then((value) => rp.close());
Isolate.spawn(_getNumbers, rp.sendPort, onExit: rp2.sendPort);
return rp.cast();
}