How to use Map constructor to create a Map in Dart

A Map is a collection of data that is organized into key-value pairs. Any type of key and value can be used. It is a growable collection, which means it can shrink and grow in run-time.

To declare a Map we can use the Map() constructor.

Syntax

var map_name = Map();
map_name[key] = value; // initialize map

Code

The following code shows how to use the Map() constructor to create a map.

void main() {
var fruits = Map();
fruits[1] = 'Apple';
fruits[4] = 'Avocado';
fruits[2] = 'Pineapple';
fruits[6] = 'Mango';
fruits[3] = 'Pawpaw';
print(fruits);
}

Note: a Map can contain a null value.

Map properties

The properties of the Map class in the dart: core package are as follows.

Map properties

S/N

Property

Description

1

Keys



Returns an iterable object representing keys

2

Values


Returns an iterable object representing values

3

Length

Returns the size of the Map


4

isEmpty


Returns true if the Map empty

5

isNotEmpty


Returns true if the Map is not empty