When executing the following code , no items added to list named x , Any idea why ? I know if i loop normally over it, it will work but i just wonder why it behaves like that
List<int> x = [];
List<int> y = [1,2,3,4];
y.map((item) => x.add(item 2));
CodePudding user response:
The .map()
method returns a lazily evaluated Iterable
:
Returns a new lazy Iterable with elements that are created by calling toElement on each element of this Iterable in iteration order.
Since you don't consume the returned Iterable
, it will not trigger the method provided to map()
and nothing happens.
You can instead use the forEach()
method which will run on each elements in your list immediately.