dart

For loop in Dart

For loop in Dart is used to iterate through dart collections. Different loops examples are explained in this code snippet.

void main() {
  //Simple for loop
  for (var i = 0; i <= 2; i++) {
    print(i);
  }
  
  //Loop through dictionary of maps
  List subjects = [
    {"id": 1, "name": "Math"},
    {"id": 2, "name": "Physics"},
    {"id": 1, "name": "Chemistry"}
  ];
  for (var sub in subjects) {
    print(sub);
  }
  
  //Loop through sets
  Set names = {"Ankit", "John", "Rick"};
  for (var name in names) {
    print(name);
  }
}
Output
0
1
2
{id: 1, name: Math}
{id: 2, name: Physics}
{id: 1, name: Chemistry}
Ankit
John
Rick
Was this helpful?