Looping Over The Element Of List

Looping Over The Element Of List

You can loop over the elements of a list using various methods

  • Using a for loop

  • Using a for-in loop

  • Using forEach method

Example

void main() {
  //using for loop
  List<int> myList = [1, 2, 3, 4, 5];
  for (int i = 0; i < myList.length; i++) {
    print(myList[i]);
  }
  //using for in loop
  List<int> myList1 = [1, 2, 3, 4, 5];
  for (int element in myList1) {
    print(element);
  }
  // using foreach loop
  List<int> myList2 = [1, 2, 3, 4, 5];
  myList2.forEach((element) {
    print(element);
  });
}

Output

1
2
3
4
5
1
2
3
4
5
1
2
3
4
5

Exited.