For in Dart

Find this useful? Support us: Star on GitHub 6
Category: Loop | Language: Dart

Sure, I'll be glad to help.

In Dart, we can use the for loop to iterate over elements in a collection. The basic syntax for the for loop is as follows:

for (var item in collection) {
  // Do something with the item
}

Here, collection can be an iterable object like List, Set, or Map. The variable item represents each element in the collection.

Let's look at some examples to understand this better:

Example 1: Iterating over List

void main() {
var numbers = [1, 2, 3, 4, 5];

for (var number in numbers) {
print(number);
}
}

Output:
1
2
3
4
5

Example 2: Iterating over Map

void main() {
var person = {'name': 'John', 'age': 30, 'city': 'New York'};

for (var key in person.keys) {
print('$key: ${person[key]}');
}
}

Output:
name: John
age: 30
city: New York

Example 3: Iterate over a range of numbers

void main() {
for (var i = 1; i <= 5; i++) {
print(i);
}
}

Output:
1
2
3
4
5

I hope this helps you understand how to use for loop in Dart. If you have any further questions or doubts, feel free to ask!