Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are lists ensured to be processed in order by a for loop?

Tags:

flutter

dart

In a Flutter application, is the order of a list always ensured?

List<MyClass> myObjects;
// ... many add operations
int i = 0;
for(MyClass myObject in myObjects) {
  assert(myObject == myObjects[i]); // will it always go through?
  i++;
}

If not, what is the most efficient way to ensure a list is maintained and processed in the right order?

like image 318
Martin Braun Avatar asked Nov 16 '25 09:11

Martin Braun


1 Answers

Yes. for-in in Dart works on Iterable objects. (It is not like for-in in JavaScript which iterates over object properties, which could be in some indeterminate order.)

From the List documentation:

Lists are Iterable. Iteration occurs over values in index order.

like image 102
jamesdlin Avatar answered Nov 19 '25 01:11

jamesdlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!