Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A Map in Dart is a collection of key-value pairs, formally defined by the generic interface Map<K, V>. It associates a unique key of type K with a value of type V. By default, Dart implements maps using LinkedHashMap, meaning the collection maintains the insertion order of its elements while providing constant-time O(1) complexity for lookups, insertions, and deletions.

Initialization and Syntax

Maps can be instantiated using map literals or constructors. Dart’s type inference automatically resolves K and V based on the provided literal values.
// Map literal with inferred type Map<String, int>
var inferredMap = {'alpha': 1, 'beta': 2};

// Explicit generic type declaration
Map<String, dynamic> explicitMap = {
  'id': 101,
  'isActive': true,
};

// Constructor initialization
var emptyMap = Map<int, String>();

Access and Mutation

Values are accessed and mutated using the subscript operator []. Because a requested key might not exist in the map, the subscript operator always returns a nullable type V?.
var config = {'retries': 3, 'timeout': 5000};

// Reading values
int? retries = config['retries']; // Returns 3
int? port = config['port'];       // Returns null

// Mutating values
config['port'] = 8080;            // Inserts a new key-value pair
config['retries'] = 5;            // Updates the value of an existing key

Core Properties

The Map interface exposes several properties that return Iterable objects, allowing for functional transformations of the map’s structural components.
  • keys: Returns an Iterable<K> containing all keys.
  • values: Returns an Iterable<V> containing all values.
  • entries: Returns an Iterable<MapEntry<K, V>>, representing both keys and values as single objects.
  • length: Returns the integer count of key-value pairs.
  • isEmpty / isNotEmpty: Boolean properties evaluating the map’s length.

Essential Methods

Dart provides built-in methods for safe mutation, validation, and transformation of map data.
var data = {'a': 1, 'b': 2};

// Adds the key-value pair only if the key does not currently exist
data.putIfAbsent('c', () => 3);

// Updates the value for a key. Throws an error if the key doesn't exist 
// unless the optional ifAbsent callback is provided.
data.update('a', (value) => value + 10, ifAbsent: () => 10);

// Removes the key and its associated value, returning the removed value (V?)
int? removedValue = data.remove('b');

// Boolean checks for existence
bool hasKey = data.containsKey('a');     // true
bool hasValue = data.containsValue(99);  // false

// Clears all entries, resulting in an empty map
data.clear();

Iteration

Maps can be traversed using the forEach method or via for-in loops operating on the entries iterable.
var metrics = {'cpu': 45, 'ram': 1024};

// Using the forEach method
metrics.forEach((key, value) {
  print('$key: $value');
});

// Using a for-in loop with MapEntry
for (MapEntry<String, int> entry in metrics.entries) {
  print('${entry.key}: ${entry.value}');
}

Alternative Implementations

While LinkedHashMap is the default, the dart:collection library provides alternative implementations for specific algorithmic requirements:
  • HashMap<K, V>: An unordered hash-table implementation. It does not guarantee iteration order but can offer marginal performance improvements over LinkedHashMap in highly volatile datasets.
  • SplayTreeMap<K, V>: A self-balancing binary search tree implementation. It requires keys to be mutually comparable and maintains the map in a sorted order based on the keys.
Master Dart with Deep Grasping Methodology!Learn More