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

Time:03-03

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?

CodePudding user response:

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.

  • Related