Search code examples
dartfor-in-loop

For-in loops order in dart


If I use for-in loop in dart on some List, are the iterations guaranteed to be in the correct order? e.g. Does the following code

List bars = getSomeListOfBars();

for(bar in bars){
    foo(bar);
}

in the exact same way as the following?

List bars = getSomeListOfBars();

for(int i=0;i<bars.length;i++){
    foo(bar[i]);
}

I have not found dart specific explanation anywhere, thank you.


Solution

  • If you use for(x in y) with a collection that guarantees the iteration order, then for(x in y) is guaranteed to iterate in this order. If the collection itself doesn't guarantee the order, then of course for(x in y) doesn't as well.