Summary: in this tutorial, you’ll learn how to use the Dart Map type to manage collections of key/value pairs.
Introduction to the Dart Map type
Map
type allows you to manage a collection of key/value pairs. The Map
type is similar to the Dictionary
type in other programming languages.
In a map, keys are unique. Each key has an associated value. Unlike keys, values can be duplicated. Generally, you cannot add or remove keys while performing an operation on the map.
Creating a map
The following creates a map where keys are strings and values are integers:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
print(fruits);
}
Code language: Dart (dart)
In this example, we define a map called fruits
which maps a string (key) to a value (integer). Since we use var
keyword, Dart will infer the type of the map as:
Map<String, int>
Code language: Dart (dart)
In this syntax, the String
denotes the type of the key and the int
represents the type of the value.
The following statement is equivalent to the one above but uses the Map<String, int>
type explicitly:
Map<String, int> fruits = {'apple': 1, 'banana': 1, 'orange': 2};
Code language: Dart (dart)
To define an empty map, you use curly braces without any elements like this:
Map<String, int> fruits = {};
Code language: Dart (dart)
A shorter way to do this is to move the type of the map to the right:
var fruits = <String, int>{};
Code language: Dart (dart)
Notice that if you don’t specify the type explicitly, Dart will infer the type as <dynamic, dynamic>
. For example:
var fruits = {};
Code language: Dart (dart)
Printing the elements of a map
To print elements of a map, you pass it to the print()
function. For example:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
print(fruits);
}
Code language: Dart (dart)
Output:
{apple: 1, banana: 2, orange: 3}
Code language: Dart (dart)
Accessing elements from a map
To access an element of a map, you use the subscript notation with a key. For example:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
print(fruits['banana']);
}
Code language: Dart (dart)
Output:
2
Code language: Dart (dart)
Adding elements to a map
To add a new element to a map, you assign the element with the key that is not available on the map:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
fruits['straberry'] = 4;
print(fruits);
}
Code language: Dart (dart)
Output:
{apple: 1, banana: 2, orange: 3, straberry: 4}
Code language: Dart (dart)
Updating elements
If you assign a value to an element with the key that already exists, you’ll update the value of the element. For example:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
fruits['apple'] = 0;
print(fruits);
}
Code language: Dart (dart)
Output:
{apple: 0, banana: 2, orange: 3}
Code language: Dart (dart)
Removing elements
To remove an element by a key, you use the remove()
method. For example:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
fruits.remove('apple');
print(fruits);
}
Code language: Dart (dart)
Output:
{banana: 2, orange: 3}
Code language: Dart (dart)
Map properties
The Map type provides some useful properties:
map.isEmpty
– return true if the map has no element.map.isNotEmpty
– return true if the map has at least one element.map.length
– return the number of elements of the map.map.keys
– return a list of keys.maps.values
– return a list of values.
For example:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
print(fruits.isEmpty); // false
print(fruits.isNotEmpty); // true
print(fruits.length); // 3
print(fruits.keys); // (apple, banana, orange)
print(fruits.values); // (1, 2, 3)
}
Code language: Dart (dart)
Output:
false
true
3
Code language: Dart (dart)
Checking for the existence of keys or values
To check if a key exists in a map, you use the containsKey()
method. For example:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
print(fruits.containsKey('apple')); // true
print(fruits.containsKey('strawberry')); //false
}
Code language: Dart (dart)
Output:
true
false
Code language: Dart (dart)
Similarly, to check if a value exists in a map, you use the containsValue()
method:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
print(fruits.containsValue(1)); // true
print(fruits.containsValue(2)); // false
}
Code language: Dart (dart)
Iterating over elements of a map
To iterate over elements of the map, you use the for-in statement. However, you cannot use it directly with the map elements but keys like this:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
for (var name in fruits.keys) {
print('$name: ${fruits[name]}');
}
}
Code language: Dart (dart)
Output:
apple: 1
banana: 2
orange: 3
Code language: Dart (dart)
Also, you can use the entries property of a map. To iterate over elements of the map, you can iterate over the map.entries
. Each MapEntry
object has two properties including key
and value
. For example:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
for (var fruit in fruits.entries) {
print('${fruit.key}: ${fruit.value}');
}
}
Code language: Dart (dart)
Output:
apple: 1
banana: 2
orange: 3
Code language: Dart (dart)
Like a List, you can use the forEach()
method:
void main() {
var fruits = {'apple': 1, 'banana': 2, 'orange': 3};
fruits.forEach((key, value) => print('$key: $value'));
}
Code language: Dart (dart)
Output:
apple: 1
banana: 2
orange: 3
Code language: Dart (dart)
Summary
- A map is a collection of key/value pairs.