5  Looping Collection

I’d be happy to guide you through Dart’s modern looping techniques! As someone with Python experience, you’ll find many similarities but also some important differences. Let’s explore Dart’s collection iteration methods and compare them with their Python counterparts.

5.1 The forEach() Method in Dart

In Dart, the forEach() method allows you to iterate through elements in a collection and perform an operation on each one.

void main() {
  List<String> fruits = ['apple', 'banana', 'orange'];
  
  // Using forEach
  fruits.forEach((fruit) {
    print('I like $fruit');
  });
  
  // Using forEach with arrow syntax
  fruits.forEach((fruit) => print('I enjoy $fruit'));
}
fruits = ['apple', 'banana', 'orange']

# Using for loop
for fruit in fruits:
    print(f'I like {fruit}')
    
# Using list comprehension (though this is for creating new lists, not just side effects)
[print(f'I enjoy {fruit}') for fruit in fruits]

The key difference is that Python’s for loop is a statement, while Dart’s forEach() takes a function as an argument. Python has no direct equivalent of forEach(), though you could use the map() function with a side effect.

5.2 The map() Method in Dart

The map() method transforms each element in a collection and returns a new iterable.

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  
  // Using map() to double each number
  var doubled = numbers.map((number) => number * 2);
  print(doubled); // (2, 4, 6, 8, 10) - returns an Iterable
  
  // Convert back to List
  List<int> doubledList = doubled.toList();
  print(doubledList); // [2, 4, 6, 8, 10]
}
numbers = [1, 2, 3, 4, 5]

# Using map function with lambda
doubled = map(lambda x: x * 2, numbers)
print(list(doubled))  # [2, 4, 6, 8, 10]

# Using list comprehension (more common in Python)
doubled_comp = [x * 2 for x in numbers]
print(doubled_comp)  # [2, 4, 6, 8, 10]

Notice that in Dart, map() returns an Iterable, not a List. You need to call .toList() to convert it. In Python, map() returns a map object that needs to be converted to a list to display all elements.

5.3 The where() Method in Dart (Similar to Python’s filter())

The where() method filters elements that satisfy a condition.

void main() {
  List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  
  // Filter even numbers
  var evenNumbers = numbers.where((number) => number % 2 == 0);
  print(evenNumbers.toList()); // [2, 4, 6, 8, 10]
}
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Using filter with lambda
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers))  # [2, 4, 6, 8, 10]

# Using list comprehension (more common)
even_comp = [x for x in numbers if x % 2 == 0]
print(even_comp)  # [2, 4, 6, 8, 10]

5.4 The reduce() Method in Dart

The reduce() method combines all elements in a collection using a function.

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  
  // Sum all numbers using reduce
  int sum = numbers.reduce((value, element) => value + element);
  print(sum); // 15
  
  // Find maximum value
  int max = numbers.reduce((curr, next) => curr > next ? curr : next);
  print(max); // 5
}
from functools import reduce
numbers = [1, 2, 3, 4, 5]

# Using reduce
sum_result = reduce(lambda x, y: x + y, numbers)
print(sum_result)  # 15

# More commonly in Python
print(sum(numbers))  # 15
print(max(numbers))  # 5

In Python, reduce() needs to be imported from the functools module, whereas in Dart it’s a built-in method on iterables.

5.5 Method Chaining in Dart

One powerful aspect of Dart’s collection methods is that they can be chained together:

void main() {
  List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  
  // Chain multiple operations
  var result = numbers
      .where((n) => n % 2 == 0)  // Get even numbers
      .map((n) => n * n)         // Square them
      .toList();                  // Convert to List
  
  print(result); // [4, 16, 36, 64, 100]
}
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Using functional approach (less common)
result = list(map(lambda x: x * x, filter(lambda x: x % 2 == 0, numbers)))

# More Pythonic approach with comprehension
result = [x * x for x in numbers if x % 2 == 0]

print(result)  # [4, 16, 36, 64, 100]

5.6 The fold() Method in Dart

The fold() method is similar to reduce() but allows you to specify an initial value and return a different type.

void main() {
  List<String> words = ['Hello', 'Dart', 'Programming'];
  
  // Count total characters using fold
  int totalChars = words.fold(0, (prev, word) => prev + word.length);
  print(totalChars); // 19
  
  // Join all words with a space
  String joined = words.fold('', (prev, word) => prev + (prev.isEmpty ? '' : ' ') + word);
  print(joined); // "Hello Dart Programming"
}
words = ['Hello', 'Dart', 'Programming']

# Using reduce for character count
from functools import reduce
total_chars = reduce(lambda acc, word: acc + len(word), words, 0)
print(total_chars)  # 19

# More Pythonic way
total_chars = sum(len(word) for word in words)
print(total_chars)  # 19

# Joining words
joined = ' '.join(words)
print(joined)  # "Hello Dart Programming"