The syntax of Map.keys property is:
Iterable<K> keys
This keys property of Map the keys of this
Map object.
Map.keys returns value of type Iterable<K>
.
In this example,
ages
containing the ages of individuals.keys
property of the map ages
to get an iterable containing all the keys.void main() {
Map<String, int> ages = {'Alice': 30, 'Bob': 25, 'Charlie': 35};
Iterable<String> mapKeys = ages.keys;
print('Keys in the map: $mapKeys');
}
Keys in the map: (Alice, Bob, Charlie)
In this example,
numbers
containing numeric mappings.keys
property of the map numbers
to get an iterable containing all the keys.void main() {
Map<int, String> numbers = {1: 'one', 2: 'two', 3: 'three'};
Iterable<int> mapKeys = numbers.keys;
print('Keys in the map: $mapKeys');
}
Keys in the map: (1, 2, 3)
In this example,
flags
containing boolean flags.keys
property of the map flags
to get an iterable containing all the keys.void main() {
Map<String, bool> flags = {'enabled': true, 'disabled': false};
Iterable<String> mapKeys = flags.keys;
print('Keys in the map: $mapKeys');
}
Keys in the map: (enabled, disabled)
In this Dart tutorial, we learned about keys property of Map: the syntax and few working examples with output and detailed explanation for each example.