0.9 C
New York
Friday, February 3, 2023

Working with For loop in Dart (and Flutter)

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

1. For-in syntax

Example: Advertisements

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 “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.

Advertisements

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles