Just as you can also use the if
keyword inside of lists (to add elements conditionally), you can also use the for
keyword to add multiple items into a list:
final numbers = [5, 6]; final myList = [ 1, 2, for (final num in numbers) num ];
In this example, the numbers 5
and 6
will be added to myList
(hence myList
thereafter is [1, 2, 5, 6]
).
This for ... in
syntax is a special variation of the for loop that loops through multiple items in a list. You will see it again later in the course - both outside and inside of a list. It will also be explained again later.
The idea behind this loop is to simplify the process of performing some operation on all items in a list.
When used in a list, it's essentially an alternative to the spread operator (...
):
final numbers = [5, 6]; final myList = [ 1, 2, ...numbers ];
It can be useful in scenarios where values must be transformed before being added to a list - the for ... in
loop can then be used instead of map()
+ spread operator:
final numbers = [5, 6]; final myList = [ 1, 2, ...numbers.map((n) { return n * 2; }) // adds 10 and 12 ];
can be replaced with:
final numbers = [5, 6]; final myList = [ 1, 2, for (final num in numbers) num * 2 // adds 10 and 12 ];
As mentioned, you will learn more about for
later in the course.
You can also learn more about for ... in
inside of lists here: https://github.com/dart-lang/language/blob/master/accepted/2.3/control-flow-collections/feature-specification.md#repetition