Now that you have a good idea at how to work with anonymous functions, it’s time to build on that by working with a list. A list comes with two different functions called map and filter. We will start with the map function.
When you use map, you will loop through every item in the collection. Your function will run on each item. At which you point, you can replace that item in the array with another. Or you can keep the existing item. Naturally, we can use our anonymous functions to make the map operation.
To get started, open up your browser and head over to dartpad.dev. We’re going to create a list of maps. Add the following:
var names = [
{ 'firstName': 'Frank', 'lastName': 'Oz' },
{ 'firstName': 'Jim', 'lastName': 'Henson' },
{ 'firstName': 'Stan', 'lastName': 'Winston' }
];
Now we have a three different maps that has a first name and a last name. Mind you, these are map collection types. We will now use the map function on the list.
First we’ll create a new variable.
var fullNames =
Now we’ll call map on the names.
var fullNames = names.map();
We’ll pass in our anonymous function. This will first take an item that represents our map collection.
var fullNames = names.map((item) {
});
Now lets get the first name and last name.
final firstName = item['firstName'];
final lastName = item['lastName'];
Then we’ll do an if-check to make sure we aren’t working with null values.
if (firstName != null && lastName != null) {
}
Finally, we return the full name.
return '$firstName $lastName';
At which point, we can print out the list of full names.
print(fullNames);
Now we’ve transformed our names into something different all while anonymous functions.
Filtering is another operation that you’ll be utilizing with your collections. The filter takes a function that returns a true or false value. It passes the item that you are filtering and you write the criteria whether to keep the item or to discard it. For this, we use the where function. The function takes in the item being filtered whereby we return a boolean value. We return true to keep it and false to filter it out. Let’s put our filter to work.
In the last demo, we used map to combine names together. In this demo, we’re going to filter out the names of people who did not work on the muppets. Based on our names, you can see we have Jim Henson and Frank OZ. But Stan Winston did not work on the muppets so let’s filter him out.
We will start by creating a variable for the muppets, using the where function on the names variable. This passes in our map.
var muppetCreators = names.where((item) {
});
Next, we get the last name of the current item.
final lastName = item['lastName'];
Since we are dealing with a map, we need to check if the last name is null.
if (lastName != null) {
return (lastName == 'Oz' || lastName == 'Henson');
}
Finally, we check if the last name is either Oz or Henson. Since the expression returns a true or false value, we return the result of the expression. In the case of a null, we return false
return false;
Let’s print out the muppet creators to see our filtered list.
print(muppetCreators);
And that’s it. Run the program. And look at that. We have our filtered list.