Spawning Isolates in for loop and processing parallelly in Dart


for(int i=0;i<2;i++){
   ..... = await someFunc(someData[i]) (this line spawns off new Isolate)

I am writing this kind of function in dart and what I expect is to get this order pr0 –>someFunc(data[0]) –> pr1 –> someFunc(data[1]) –> post processing of whichever (Isolate finishes first ). But I know that I cant get the same effect using for loop. It is giving me of the order pr0 –> someFunc(data[0]) –> pr1 –> someFunc(data[1]) –> po0 –> po1.
How should I implement to get the above effect.

Thanks in advance.


You could try using Future.wait instead (

final results = await Future.wait([
  for (int i = 0; i < 2; i++) someFunc(someData[i]),

Answered By – mmcdon20

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

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