Working with For loop in Dart (and Flutter)

Updated: February 6, 2023 By: Napoleon Post a comment

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.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles