The syntax of Map.length property is:
int length
This length property of Map the number of key/value pairs in the map.
Map.length returns value of type int
.
In this example,
map1
with key/value pairs {'apple': 1, 'banana': 2, 'cherry': 3}.length
property.void main() {
Map<String, int> map1 = {'apple': 1, 'banana': 2, 'cherry': 3};
int mapLength = map1.length;
print('Map length: $mapLength');
}
Map length: 3
In this example,
map2
.length
property.void main() {
Map<int, String> map2 = {};
int mapLength = map2.length;
print('Map length: $mapLength');
}
Map length: 0
In this example,
map3
with a single key/value pair {'name': 'John'}.length
property.void main() {
Map<String, String> map3 = {'name': 'John'};
int mapLength = map3.length;
print('Map length: $mapLength');
}
Map length: 1
In this Dart tutorial, we learned about length property of Map: the syntax and few working examples with output and detailed explanation for each example.