Map Length

Map length property returns the number of key-value pairs in this Map.

length is a read only property. We can only use it in an expression, or store this length value into a variable, but cannot assign it with a new value.

Syntax

The syntax to get the length of a Map myMap is

myMap.length

Examples

Get length of a Map

In the following Dart Program, we take a Map myMap with three key-value pairs, and get its length programmatically using length property of the Map class.

main.dart

</>
Copy
void main() {
    var myMap = {'apple': 25, 'banana': 10, 'cherry': 6};
    var myMapLength = myMap.length;
    print('Length : $myMapLength');
}

Output

Length : 3

Length of an Empty Map

Since an empty Map contains no entries, length property for an empty Map must return zero, which we shall verify programmatically in the following Dart program.

main.dart

</>
Copy
void main() {
    var myMap = {};
    var myMapLength = myMap.length;
    print('Length : $myMapLength');
}

Output

Length : 0

Conclusion

In this Dart Tutorial, we learned how to get the length of a Map, using Map.length property.