
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:
- Dart: Convert Class Instances (Objects) to Maps and Vice Versa
- 2 Ways to Get a Random Item from a List in Dart (and Flutter)
- How to Check if a Variable is Null in Flutter and Dart
- How to Flatten a Nested List in Dart
- Inheritance in Dart: A Quick Example
- Flutter & Dart: 3 Ways to Generate Random Strings
- Using Static Methods in Dart and Flutter
You can also check out our Flutter category page or Dart category page for the latest tutorials and examples.