Dart For...in loop

Dart For...in loop

Let's start with the basics.

Its syntax is relatively simple:

for (var item in collection) {
  // Body of the loop
}

Here's a breakdown of the syntax:

  • for: This keyword signals the start of the loop.

  • var item: This declares a loop variable (item) that represents each element or key in the collection during each iteration. You can replace var with the specific type if you know the type of the elements in the collection.

  • in: This keyword is used to specify the collection over which the loop iterates.

  • collection: This is the collection over which the loop iterates. It can be a list, set, map, or any iterable object.


Here is an example of using a for...in loop with a list.