Iterating Over Collections in Dart

Dart provides various ways to iterate over collections such as lists, sets, and maps. Understanding how to efficiently traverse these collections is vital for data processing and manipulation.

Iterating Over Lists

Using for Loop

The traditional for loop allows you to iterate over the indices of the list.

void main() {
  List<String> fruits = ['Apple', 'Banana', 'Cherry'];

  for (int i = 0; i < fruits.length; i++) {
    print(fruits[i]); // Output: Apple, Banana, Cherry   }
}

Using forEach Method

The forEach method executes a function for each element in the list.

void main() {
  List<String> fruits = ['Apple', 'Banana', 'Cherry'];

  fruits.forEach((fruit) {
    print(fruit); // Output: Apple, Banana, Cherry   });
}

Using for...in Loop

The for...in loop is another concise way to iterate through elements.

void main() {
  List<String> fruits = ['Apple', 'Banana', 'Cherry'];

  for (var fruit in fruits) {
    print(fruit); // Output: Apple, Banana, Cherry   }
}

Iterating Over Sets

Using forEach Method

The forEach method can also be used with sets.

void main() {
  Set<String> fruits = {'Apple', 'Banana', 'Cherry'};

  fruits.forEach((fruit) {
    print(fruit); // Output: Apple, Banana, Cherry (order may vary)   
    });
}

Using for...in Loop

You can use the for...in loop to iterate over sets as well.

void main() {
  Set<String> fruits = {'Apple', 'Banana', 'Cherry'};

  for (var fruit in fruits) {
    print(fruit); // Output: Apple, Banana, Cherry (order may vary)   
    }
}

Iterating Over Maps

Using forEach Method

You can iterate over the entries of a map using the forEach method.

void main() {
  Map<String, int> ages = {
    'Alice': 30,
    'Bob': 25,
    'Charlie': 35,
  };

  ages.forEach((name, age) {
    print('$name is $age years old.'); // Output: Alice is 30 years old. ...   
    });
}

Using for...in Loop on Entries

You can also use the for...in loop to iterate through the entries of a map.

void main() {
  Map<String, int> ages = {
    'Alice': 30,
    'Bob': 25,
    'Charlie': 35,
  };

  for (var entry in ages.entries) {
    print('${entry.key} is ${entry.value} years old.'); // Output: Alice is 30 years old. ...  
     }
}

Iterating Over Keys or Values

You can specifically iterate over the keys or values of a map.

void main() {
  Map<String, int> ages = {
    'Alice': 30,
    'Bob': 25,
    'Charlie': 35,
  };

  // Iterating over keys   
  for (var name in ages.keys) {
    print(name); // Output: Alice, Bob, Charlie   
    }

  // Iterating over values   
  for (var age in ages.values) {
    print(age); // Output: 30, 25, 35   
    }
}

Conclusion

Iterating over collections in Dart is straightforward, with various methods available to suit different needs. Whether you're working with lists, sets, or maps, Dart provides flexible and efficient ways to access and manipulate data. Mastering these iteration techniques will enhance your ability to handle collections effectively in your Dart applications

PLAY QUIZ

Which of the following methods allows you to iterate over the indices of a list in Dart?

forEach

for...in loop

for loop

map