Programming in Dart: Control Flow & Collections

May 3 2022 · Dart 2.15, DartPad, DartPad

Part 2: More Collections

13. Access & Work with Maps

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 12. Create & Populate Maps Next episode: 14. Challenge: Work with Maps

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 13. Access & Work with Maps

Apple’s Swift Dictionary documentation: https://api.dart.dev/stable/2.14.4/dart-core/Map-class.html

Transcript: 13. Access & Work with Maps

In the previous episode, you learned about how to create maps, how key-value storage works, and how to add and update elements in your maps. But there’s a whole lot more to maps than just that!

Maps have many of the same qualities as lists; you can use their built-in methods to check to see if they’re empty, how many key-value pairs they contain, or if a map contains a a particular value.

But what happens when you try to access a value in a map that doesn’t exist?

Remember that Dart provides a way to tell if there is a missing. The map returns a null value which means, there was no value for your key. This means, whenever you access a map, you should always keep null values in mind.

Open up Dartpad.dev in your browser, and lets add our friends back to a map.

var namesAndPets = { 'Ron': '🐀 Rat', 'Rincewind': '🛄 Luggage', 'Thor': '🔨 Hammer'  }; 

Now lets print out the value for Rincewind. To do this, we simply put the key in brackets.

print(namesAndPets['Rincewind']);

Run the program. And we get our value. But what about keys that don’t exist. Let’s see what pet Captain Ahab has.

print(namesAndPets['Captain Ahab']);

Now run the program. This time we get a null value. Captain Ahab isn’t a key in our dictionary. So how do we provide a value if one isn’t present?

Welcome to your next null operator. In the case want to return default value in the case of null, you use the if-null operator. This is just two question marks together. Here you provide a value on the left and a value on the right. If the value on the right is null, then then the operator will use value on the right instead. Let’s put it to use with Captain Ahab.

Okay, let’s use the if-null operator. We will print a string. Let’s update our print to the following.

print(namesAndPets['Captain Ahab'] ?? 'No white whale for Captain Ahab');

Now run the program. This time, instead of a null value, we get our text. As you can see that if-null operator is pretty useful. We also have useful properties on maps. For example, if you want to see if a map is empty, use the isEmpty method.

print(namesAndPets.isEmpty);

We can also see how many items are contained in the map by using the length property.

print(namesAndPets.isEmpty);

To remove an item, you use the remove method. Let’s remove Ron from our map.

var pet = namesAndPets.remove('Ron');

When we remove an item, that item is returned to us in case we need to use it. Now lets print out the results.

print(pet);
print(namesAndPets);

Now run the program. Here we get Ron’s rat printed out then the names and pets with Ron removed. Now comes the fun part - iterating over all the values.

When looping over maps, you have three different options. You can loop over all the keys, all the values, and all the keys and values. For this, we’ll use a for-in loop. First we’ll get the values.

for (var pet in namesAndPets.values) {
    print(pet);
}

For this, we fetch the values property and print it out. Run the program. And there are our values. For the keys, we use the keys property.

for (var name in namesAndPets.keys) {
    print(name);
}

Now you can see all our keys. Now for both the keys and values. For this, we use the entries property. It returns an entry object that contains a key property and a value property.

for (var entry in namesAndPets.entries) {
    print('key: ${entry.key} value: ${entry.value}');
}

Now run the program. And with that, we have all our keys and values.