Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Collection For and Advanced Iteration | Loops and Iteration Patterns
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Dart Control Flow

bookCollection 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

main.dart

copy
12345678
void 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

main.dart

copy
1234567891011
void 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.

question mark

Which statement best describes the main advantage of using collection for inside Dart collection literals, compared to traditional for loops?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookCollection For and Advanced Iteration

Veeg om het menu te tonen

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

main.dart

copy
12345678
void 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

main.dart

copy
1234567891011
void 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.

question mark

Which statement best describes the main advantage of using collection for inside Dart collection literals, compared to traditional for loops?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
some-alt