Search⌘ K

Working with Maps

Explore how to work with maps in Dart, including adding key-value pairs, accessing values, checking keys, retrieving all keys and values, and removing pairs. This lesson helps you understand the essential properties and methods that maps offer for managing data efficiently in Flutter development.

Just like lists and sets, maps are objects that have associated properties and methods. Let’s take a look at some of them below.

Adding Key-value pairs

To add a new key-value pair to a map that has already been declared, use the following syntax:

Dart
mapName[key]=value

Let’s add some key-value pairs to the numbers map we have declared in the widget below.

Dart
main() {
var numbers = Map<int, String>();
numbers[1] = 'one';
numbers[2] = 'two';
numbers[3] = 'three';
print(numbers);
}

In the code snippet above, we added 3 key-value pairs to numbers.

Finding the number of pairs in a map

Just as we discussed for lists and sets, ...