Dart Access Map Items
Dart Access Map Items
You get a value from a map by writing its key inside square brackets []:
Example
void main() {
var car = {
'brand': 'Ford',
'model': 'Mustang',
};
print(car['brand']);
print(car['model']);
}
Try it Yourself »
Result:
Ford
Mustang
Missing Keys Return null
If the key is not in the map, you get null:
Example
void main() {
var car = {
'brand': 'Ford',
'model': 'Mustang',
};
print(car['year']);
}
Try it Yourself »
Result:
null
Because of this, the type of car['brand'] is String?, not String.
The question mark means the value can be null, as you learned in Null Safety.
So you cannot put it straight into a String variable:
Example
void main() {
var car = {'brand': 'Ford'};
String brand = car['brand'];
print(brand);
}
Try it Yourself »
Result:
main.dart:3:21: Error: A value of type 'String?' can't be assigned to a variable of type 'String'.
String brand = car['brand'];
^
A simple fix is to give a default value with the ?? operator:
Example
void main() {
var car = {'brand': 'Ford'};
String brand = car['brand'] ?? 'unknown';
String color = car['color'] ?? 'unknown';
print(brand);
print(color);
}
Try it Yourself »
Result:
Ford
unknown
Check if a Key Exists
Use containsKey() to check if a key is in the map.
Use containsValue() to check if a value is in the map:
Example
void main() {
var stock = {'apples': 12, 'pears': 0};
print(stock.containsKey('apples'));
print(stock.containsKey('kiwis'));
print(stock.containsValue(0));
}
Try it Yourself »
Result:
true
false
true
Add and Change Items
To add a new item, write a new key in square brackets and give it a value.
A new key is added at the end of the map.
If the key already exists, the old value is replaced:
Example
void main() {
var car = {
'brand': 'Ford',
'model': 'Mustang',
};
car['color'] = 'red';
car['model'] = 'Focus';
print(car);
}
Try it Yourself »
Result:
{brand: Ford, model: Focus, color: red}
putIfAbsent() adds an item only if the key is not in the map yet.
The value is given by a small function, written as () => value:
Example
void main() {
var stock = {'apples': 12};
stock.putIfAbsent('apples', () => 50);
stock.putIfAbsent('pears', () => 7);
print(stock);
}
Try it Yourself »
Result:
{apples: 12, pears: 7}
update() changes the value of a key, based on its old value:
Example
void main() {
var stock = {'apples': 12, 'pears': 7};
stock.update('apples', (value) => value + 3);
print(stock);
}
Try it Yourself »
Result:
{apples: 15, pears: 7}
If the key does not exist, update() throws an error.
Add ifAbsent to give a value to use when the key is missing:
Example
void main() {
var stock = {'apples': 12};
stock.update('kiwis', (value) => value + 1, ifAbsent: () => 1);
print(stock);
}
Try it Yourself »
Result:
{apples: 12, kiwis: 1}
Note: The arrow => creates a short function.
In () => 50, the function gets no value, and in (value) => value + 3, it gets the old value in value.
You will learn more about it in Arrow Functions.
Remove Items
Use remove() to remove a key and its value.
Use clear() to remove all items:
Example
void main() {
var car = {
'brand': 'Ford',
'model': 'Mustang',
'color': 'red',
};
car.remove('color');
print(car);
car.clear();
print(car);
}
Try it Yourself »
Result:
{brand: Ford, model: Mustang}
{}