List.map not executing despite List having elements inside(flutter)

This is because of the way map method works. According to the documentation –

map<T> method

Iterable<T> map <T>(

    T f(
        E e
    )

) 

Returns a new lazy Iterable with elements that are created by calling f on each element of this Iterable in iteration order.

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the returned Iterable will invoke the supplied function f multiple times on the same element.

Methods on the returned iterable are allowed to omit calling f on any element where the result isn’t needed. For example, elementAt may call f only once.

In your example, because you are not iterating over the resulting Iterable, it won’t invoke the function. So if you change your code to this

snap.docs.map((e) => print(e.data().toString())).toList();

then it will execute because now it needs to iterate over the resulting Iterable in order to convert it to a List.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top