Kinda Code
Home/Dart/Working with For loop in Dart (and Flutter)

Working with For loop in Dart (and Flutter)

Last updated: February 06, 2023

The for loop is one of the most important features in many programming languages including Dart.

1. For-in syntax

Example:

void main() {
  final myList = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  for (var item in myList) {
    print(item);
  }
}

Output:

1
2
3
4
5
6
7
8
9

2. A different syntax of the “for” loop in Dart

Example:

void main() {
  final myList = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  for(var i = 0; i < myList.length; i++){
    print(myList[i]);
  }
}

Output:

1
2
3
4
5
6
7
8
9

Further reading:

You can also check out our Flutter category page or Dart category page for the latest tutorials and examples.

Related Articles