Collection For and Advanced Iteration
Dart provides a unique way to build and transform collections using the collection for feature, which allows you to use a for loop directly inside collection literals such as lists, sets, and maps. This pattern makes it easy to generate new collections based on existing data, especially when you want to apply conditions or transformations as you build the collection. With collection for, you can filter, map, or otherwise process items concisely, all within the collection declaration itself.
main.dart
12345678void main() { List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; List<int> evenNumbers = [ for (var n in numbers) if (n % 2 == 0) n ]; print('Even numbers: $evenNumbers'); }
In the example above, you use collection for to create a new list called evenNumbers by iterating through each element in the numbers list. The if (n % 2 == 0) inside the list literal acts as a filter, adding only even numbers to the new list. This approach is both compact and expressive, letting you build lists based on conditions or transformations without writing a separate loop or temporary variables. Collection for is especially powerful for filtering and mapping because it keeps your code concise and readable, reducing boilerplate and making the intent clear at a glance.
main.dart
1234567891011void main() { List<int> numbers = [3, 7, 12, 18, 21, 29]; int threshold = 15; List<int> filteredNumbers = []; for (var n in numbers) { if (n > threshold) { filteredNumbers.add(n); } } print('Numbers greater than $threshold: $filteredNumbers'); }
Comparing collection for with a traditional for loop, you can see that collection for allows you to express filtering and mapping logic directly within the collection literal, as shown in the even numbers example. This reduces the need for separate variables and repetitive code. In contrast, the standard for loop, like the one filtering numbers greater than a threshold, requires more lines and explicit state management. Collection for is generally preferred when you want to build a new collection from existing data using a clear, concise, and declarative approach.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Can you show me an example of using collection for in Dart?
What are some other use cases for collection for besides filtering?
How does collection for compare to other collection manipulation methods in Dart?
Fantastiskt!
Completion betyg förbättrat till 10
Collection For and Advanced Iteration
Svep för att visa menyn
Dart provides a unique way to build and transform collections using the collection for feature, which allows you to use a for loop directly inside collection literals such as lists, sets, and maps. This pattern makes it easy to generate new collections based on existing data, especially when you want to apply conditions or transformations as you build the collection. With collection for, you can filter, map, or otherwise process items concisely, all within the collection declaration itself.
main.dart
12345678void main() { List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; List<int> evenNumbers = [ for (var n in numbers) if (n % 2 == 0) n ]; print('Even numbers: $evenNumbers'); }
In the example above, you use collection for to create a new list called evenNumbers by iterating through each element in the numbers list. The if (n % 2 == 0) inside the list literal acts as a filter, adding only even numbers to the new list. This approach is both compact and expressive, letting you build lists based on conditions or transformations without writing a separate loop or temporary variables. Collection for is especially powerful for filtering and mapping because it keeps your code concise and readable, reducing boilerplate and making the intent clear at a glance.
main.dart
1234567891011void main() { List<int> numbers = [3, 7, 12, 18, 21, 29]; int threshold = 15; List<int> filteredNumbers = []; for (var n in numbers) { if (n > threshold) { filteredNumbers.add(n); } } print('Numbers greater than $threshold: $filteredNumbers'); }
Comparing collection for with a traditional for loop, you can see that collection for allows you to express filtering and mapping logic directly within the collection literal, as shown in the even numbers example. This reduces the need for separate variables and repetitive code. In contrast, the standard for loop, like the one filtering numbers greater than a threshold, requires more lines and explicit state management. Collection for is generally preferred when you want to build a new collection from existing data using a clear, concise, and declarative approach.
Tack för dina kommentarer!