user2522115 user2522115 - 4 months ago 24
Dart Question

For-in loops order in dart

if i use

for-in
loop in dart on some
List
, are the itterations 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

List bars = getSomeListOfBars();

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


? I have not found
dart
specific explanation anywhere, tthank you.

Answer

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.