Maps

Map is a collection of key-value pairs. Keys in a map must be unique, and value in a key-value pair can be accessed using key.

Dart has Map class to implement Maps.

Example

The following is a simple example for a Map in Dart.

</>
Copy
{'apple': 25, 'banana': 10, 'cherry': 6}

The following are some of the points that can be made from the above Map.

  • there are three key-value pairs.
  • key-value pairs are separated by comma.
  • key-value pairs are enclosed in curly braces.
  • 'apple': 25 is a key value pair, of which 'apple' is the key, and 25 is the value.

Properties

  • entriesDart Tutorial for Map.entries property. This read-only property returns an Iterator to the key-value pairs in this Map.
  • isEmptyDart Tutorial for Map.isEmpty property. This read-only property returns a boolean value of true if this Map is empty, or false if this Map is not empty.
  • isNotEmptyDart Tutorial for Map.isNotEmpty property. This read-only property returns a boolean value of true if this Map is not empty, or false if this Map is empty.
  • keysDart Tutorial for Map.keys property. This read-only property returns an Iterator to the keys in this Map.
  • lengthDart Tutorial for Map.length property. This read-only property returns the number of key-value pairs in this Map
  • valuesDart Tutorial for Map.values property. This read-only property returns an Iterator to the values in this Map.

Methods

  • addAll()Dart Tutorial for Map.addAll() method. This method adds all the key-value pairs of other Map to this Map.
  • addEntries()Dart Tutorial for Map.addEntries() method. This method adds all the entries in the given Iterable<MapEntry> to this Map.
  • clear()Dart Tutorial for Map.clear() method. This method removes all the entries from this Map.
  • containsKey()Dart Tutorial for Map.containsKey() method. This method checks if the given key is present in this Map.
  • containsValue()Dart Tutorial for Map.containsValue() method. This method checks if the given value is present in this Map.
  • forEach()Dart Tutorial for Map.forEach() method. This method executes an action (or function) for each key-value pair in this Map.
  • map()
  • putIfAbsent()
  • remove()
  • removeWhere()
  • update()
  • updateAll()