In the first example, we used the map function. map expects you to take the data element as the input of your function and then transform it into something else. It is very common to map some JSON data that your app received from an API to a strongly typed Dart object:
// Without the map function, we would usually write
// code like this
final names = <Name>[];
for (Map rawName in data) {
final first = rawName['first'];
final last = rawName['last'];
final name = Name(first, last);
names.add(name);
}
// But instead it can be simplified and it can
// actually be more performant on more complex data
final names = data.map<Name>((Map rawName) {
final first = rawName['first'];
final last = rawName['last'];
return Name(first, last);
}).toList();
Both samples achieve the same result. In the first option, you create a list that will hold the names. Then, you iterate through each entry in the data list, extract the elements from Map, initialize a named object, and then add it to the list.
The second option is certainly easier for the developer. Iterating and adding are delegated to the map function. All you need to do is tell the map function how you want to transform the element. In this case, the transformation was extracting the values and returning a Name object. map is also a generic function. Consequently, you can add some typing information – in this case, <Name> – to tell Dart that you want to save a list of names, not a list of dynamics.
This example is also purposefully verbose, although you could simplify it even more:
final names = data.map<Name>(
(raw) => Name(raw['first'], raw['last']),
).toList();
This may not seem like a big deal for this simple example, but when you need to parse complex graphs of data, these techniques can save you a lot of work and time.