dart

Loop through a List in Flutter [Dart]

To access list items in Flutter or dart, we can use the index of the item. But if you want to iterate over the list, you can use the code examples explained in this article.

List names = ["James", "Carl", "Rick", "Tony", "Justin"];

for(var name in names) {
  print(name);
}

We will use multiple methods in this post to iterate over a List in Dart or Flutter. Lists are a mutable collection in Dart. You can add or remove data to a Dart List. The items can be accessed using the index.

Loop through a list using For Loop and Item index

We know that Dart For loop can be used to iterate over a collection. And we can get the list items using its index. We will use the loop and index of items to iterate over a List here.

We can also access the item index inside each iteration using this method

Syntax

for (var i=0; i < List.length; i++) {
  List[i]
}

Code Example

List subjects = ["Math", "Physics", "Chemistry", "Hindi", "English"];

for (var i=0; i < subjects.length; i++) {
  print(subjects[i]);
}

Output

Math
Physics
Chemistry
Hindi
English

Iterate over a list using For Loop and 'in' operator

We have already checked the For Loop to iterate over a List in the previous code example. But we have used the index of the item in the previous code example. Here we will use the "in" operator inside For loop to loop through List items.

Syntax

for (final item in List) {
   item
}

Code Example - Iterate over a flat list

List subjects = ["Math", "Physics", "Chemistry", "Hindi", "English"];

for (var item in subjects) {
  print(item);
}

Output

Math
Physics
Chemistry
Hindi
English

Code Example - Iterate over a list of sets using for loop

List products = [
  {"id": 1, "name": "P01"},
  {"id": 2, "name": "P02"},
  {"id": 3, "name": "P03"}
];

for (final item in products) {
  print("Id is: ${item["id"]}. Name is: ${item["name"]}");
}

Output

Id is: 1. Name is: P01
Id is: 2. Name is: P02
Id is: 3. Name is: P03

Loop through a list using the while loop

The use of a while loop to iterate over a list is quite useful. The loop will be executed until the counter variable is less than the length of the List. We are getting the length of the List using List.length.

The counter variable needs to be defined using the var keyword because it will increase in each iteration.

Syntax

var i = 0;
while(i < List.length){
  print(List[i]);
  i++;
}

Code Example

List numbers = [10, 20, 30, 40, 50];

var i = 0;
while(i < numbers.length){
  print(numbers[i]);
  i++;
}

Output

10
20
30
40
50

Loop through a List using List.forEach() function

The List.forEach() function can be used to loop through a list. You can pass a callback function to it and inside it, we can access the items of the list in each iteration.

Code Example

List names = ["Tony", "Soham", "Sonia", "Prat"];

names.forEach((item) {
   print(item);
});

Output

Tony
Soham
Sonia
Prat

The above code can be written in one line using lambda expressions

List names = ["Tony", "Soham", "Sonia", "Prat"];

names.forEach((item) => print(item));

Iteration over a List of Maps using List.forEach() function

List subjects = [
  {"id": 1, "name": "John"},
  {"id": 2, "name": "Rick"},
  {"id": 3, "name": "Carol"}
];

subjects.forEach( (item) => print("Id is: ${item['id']} and Name is: ${item['name']}") );

Or you can also write the above code as below

List subjects = [
  {"id": 1, "name": "John"},
  {"id": 2, "name": "Rick"},
  {"id": 3, "name": "Carol"}
];

subjects.forEach( (item) {
  print("Id is: ${item['id']} and Name is: ${item['name']}");
});

Output

Id is: 1 and Name is: John
Id is: 2 and Name is: Rick
Id is: 3 and Name is: Carol

Conclusion

These were some methods that can be used to loop through a List in Flutter or Dart. We tried to cover all the useful code examples and methods that can be helpful but if you know more methods or examples that can be useful to iterate over the List. you can click on the below button and contribute your code for this Dart problem.

Was this helpful?