can you run Isolate.spawn multiple times?

Issue

here is a simple code that I use to learn isolate, I spawn twice, but the second spawn does not show anything, any mistake here? Thanks

import 'dart:isolate';
Future<void> main() async {
  print('start');
  await Isolate.spawn(echo, 'Dart');
  await Isolate.spawn(echo, 'Flutter'); // why this 2nd spawn not showing up?
  print('end');
}

void echo(msg) {
  print(msg);
}

Solution

Your program quits before the Isolate has done its job. You can confirm this if you add

await Future.delayed(Duration(seconds: 1));

somewhere towards the end of your program.

Setting up Isolates is often a bit challenging, with all the SendPort stuff etc.

Answered By – Eiko

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *