thumbnail

Maps In Dart

Published on 30 March 2020
Last Updated on 30 March 2020

What are maps in Dart

Maps in Dart are a set of key and value pairs. Key and Value can be any object. Keys given to Map objects are unique however values given to specific key objects don’t have to be unique.

Program to demonstrate use of Map in Dart programming language

// Program to demonstrate use of Map in Dart programming language
void main(List<String> args) {
  var numberWordMap = {
    1: 'one',
    2: 'two',
    3: 'three',
  };
  print('numberWordMap is ${numberWordMap}');
  var wordNumberMap = {
    'one': 1,
    'two': 2,
    'three': 3,
  };
  print('wordNumberMap is ${wordNumberMap}');

  // initializing an empty map
  var emptyMap = new Map();

  // initializing an empty map of String key value pairs
  var emptyMapStringString = new Map<String, String>();
  print('emptyMapStringString is ${emptyMapStringString}');

  // initializing an empty map of int, String key value pairs
  var emptyMapIntString = new Map<int, String>();
  print('emptyMapIntString is ${emptyMapIntString}');

  // modifying an empty map at runtime
  emptyMapIntString[1] = "one";
  emptyMapIntString[2] = "two";
  emptyMapIntString[3] = "three";

  print('emptyMapIntString is ${emptyMapIntString}');

  // creating a map that is a compile time constant
  const compileTimeConstantMap = {
    1: 'h',
    2: 'e',
    3: 'l',
    4: 'l',
    5: 'o',
    6: null,
  };
  print('compileTimeConstantMap is ${compileTimeConstantMap}');
}

Above program will produce following output:

numberWordMap is {1: one, 2: two, 3: three}
wordNumberMap is {one: 1, two: 2, three: 3}
emptyMapStringString is {}
emptyMapIntString is {}
emptyMapIntString is {1: one, 2: two, 3: three}
compileTimeConstantMap is {1: h, 2: e, 3: l, 4: l, 5: o, 6: null}

How to define a Map Object in Dart

Following are the various ways by which you can define a Map object in Dart

Defining a map that contains existing values in Dart:

var numberWordMap = {
  1: 'one',
  2: 'two',
  3: 'three',
};

Defining an empty map:

// initializing an empty map
var emptyMap = new Map();

// initializing an empty map of String key value pairs
var emptyMapStringString = new Map<String, String>();

// initializing an empty map of int, String key value pairs
var emptyMapIntString = new Map<int, String>();

Map objects defined using above methods, do not contain any of the existing key value pairs.

Adding new items to existing Map objects in Dart

Following code demonstrates how to add new items or modify existing items in a Dart Map object.

// modifying an empty map at runtime
emptyMapIntString[1] = "one";
emptyMapIntString[2] = "two";
emptyMapIntString[3] = "three";

While modifying or adding new items to an existing Map object, make sure that data type of objects that are being added to the existing Map match the type definition of the original Map object that was initialized.

If there is a type mismatch while adding new items to existing Map object, then it can cause a runtime error.