What is map.length method in Dart?

The map.length function in Dart returns the number of elements (key-value pairs) in the map. In short, this function is used to get the current size of the map.

Figure 1 shows the visual representation of the map.length function:

Figure 1: Visual representation of map.length function

Syntax

int map_name.length
// where the map_name is the name of the map

Parameter

This function does not require a parameter.

Return value

This function returns the number of elements in the map and is used to get the current size of the map.

Example

The following code shows how to use map.length function in Dart:

import 'dart:convert';
void main() {
var map_1 = new Map();
map_1 [1] = 'Tom';
map_1 [2] = 'Alsvin';
map_1 [3] = 'Eddie';
//map containg key value pairs
print("map_1 length: ${map_1.length}");
//map empty
var map_2 = new Map();
print("map_2 length: ${map_2.length}");
}

Free Resources