Dart List – Iterate over Elements
Dart provides several ways to iterate over the elements of a List. The best approach depends on whether you need only the element value, the element index, loop control such as break or continue, or an explicit Iterator.
For most list traversal tasks, use a for-in loop when you need each value, a traditional for loop when you need the index, and forEach() when a short callback expresses the operation clearly.
Choose a Dart List Iteration Method
| Method | Use it when | Index available | break/continue |
|---|---|---|---|
for-in | You need each list element directly | No | Yes |
Indexed for loop | You need both the index and element | Yes | Yes |
forEach() | You want to run a callback for every element | No | No |
asMap().entries | You want index-value pairs in a for-in loop | Yes | Yes |
Iterator with while | You need manual control over iterator advancement | No | Yes |
Iterate over Dart List using a For Loop and Index
Use an indexed for loop when the position of each element matters. Dart list indexes start at 0, so the loop continues while the index is less than myList.length.
Dart Program
void main(){
//list
var myList = [25, 63, 84];
//traverse through each element of list
for(var i=0;i<myList.length;i++){
print(myList[i]);
}
}
Output
D:\tutorialkart\workspace\dart_tutorial>dart example.dart
25
63
84
During each iteration, i identifies the current position and myList[i] returns the element stored at that position.
Iterate over Dart List Values using for-in
A for-in loop is usually the clearest option when you need each element but do not need its index.
void main() {
var myList = [25, 63, 84];
for (var element in myList) {
print(element);
}
}
Output
25
63
84
Unlike an indexed loop, this form gives the current value directly and avoids list indexing syntax.
Iterate over Dart List using forEach()
The forEach() method calls a function once for every element in the list. It is suitable for simple operations that do not require break or continue.
Dart Program
void main(){
var myList = [24, 63, 84];
myList.forEach((element) =>
print(element)
);
}
Output
D:\tutorialkart\workspace\dart_tutorial>dart example.dart
25
63
84
The list in this program contains 24, 63, and 84. Therefore, when you run the code, the first printed value is 24. The existing output block above shows 25, which does not match the program input.
You can also pass each element to a user-defined function instead of calling print() directly.
Dart Program
void processingFunc(var element){
//processing or transformation on the element
var x=element%2;
print(x);
}
void main(){
var myList = [24, 63, 84];
myList.forEach((element) =>
processingFunc(element)
);
}
Output
D:\tutorialkart\workspace\dart_tutorial>dart example.dart
0
1
0
Here, processingFunc() calculates the remainder after dividing each element by 2. A result of 0 indicates an even number, while 1 indicates an odd number.
Iterate with Both Index and Value using asMap().entries
To use a for-in loop while still accessing each index, convert the list view to map entries with asMap().entries. Each entry contains a key for the index and a value for the list element.
void main() {
var fruits = ['Apple', 'Banana', 'Mango'];
for (var entry in fruits.asMap().entries) {
print('${entry.key}: ${entry.value}');
}
}
Output
0: Apple
1: Banana
2: Mango
Stop or Skip Elements while Iterating a Dart List
Use a regular loop instead of forEach() when you need break to stop iteration or continue to skip an element.
void main() {
var numbers = [10, 15, 20, 25, 30];
for (var number in numbers) {
if (number == 15) {
continue;
}
if (number == 25) {
break;
}
print(number);
}
}
Output
10
20
The loop skips 15, prints 20, and stops before printing 25.
Iterate over Dart List using Iterator and While Loop
Every Dart list is iterable. Its iterator provides moveNext() to advance through the sequence and current to read the current element. This lower-level approach is useful when you specifically need manual iterator control.
Dart Program
void main(){
//list
var myList = [25, 63, 84];
//get iterator to the list
var myListIter = myList.iterator;
//iterate over the list
while(myListIter.moveNext()){
print(myListIter.current);
}
}
Output
D:\tutorialkart\workspace\dart_tutorial>dart example.dart
25
63
84
Avoid Modifying List Length during Dart Iteration
Do not add or remove elements from a growable list while iterating over that same list with for-in, forEach(), or its iterator. Changing the list length during traversal can cause a concurrent modification error.
When elements must be removed based on a condition, use a list method designed for that operation, such as removeWhere().
void main() {
var numbers = [10, 15, 20, 25];
numbers.removeWhere((number) => number.isOdd);
print(numbers);
}
Output
[10, 20]
Dart List Iteration Questions
Which loop is best for iterating over a Dart List?
Use for-in when you only need element values. Use an indexed for loop or asMap().entries when you also need indexes. Use forEach() for short callback-based operations that do not need loop control.
Can I get the index inside Dart forEach()?
List.forEach() passes only the element to its callback. For an index and value together, iterate over list.asMap().entries or use a traditional indexed for loop.
Can I use break inside Dart forEach()?
No. A break statement cannot stop a forEach() callback. Use for-in, an indexed for loop, or a while loop when early termination is required.
Does Dart List iteration preserve element order?
Yes. Standard list iteration visits elements in index order, starting at index 0 and continuing to the final element.
Summary of Dart List Iteration
In this Dart Tutorial, we learned how to iterate over elements of a Dart List using an indexed for loop, a for-in loop, forEach(), asMap().entries, and an explicit iterator with a while loop. Use the method that provides the index access and loop control required by your task.
TutorialKart.com