Dart Documentationdart.pkg.collection.wrappersDelegatingMap<K, V>

DelegatingMap<K, V> class

Creates a Map that delegates all operations to a base map.

This class can be used hide non-Map methods of an object that extends Map, or it can be extended to add extra functionality on top of an existing map object.

class DelegatingMap<K, V> implements Map<K, V> {
 Map<K, V> _base;
 DelegatingMap(Map<K, V> base) : _base = base;

 V operator [](Object key) => _base[key];

 void operator []=(K key, V value) {
   _base[key] = value;
 }

 void addAll(Map<K, V> other) {
   _base.addAll(other);
 }

 void clear() {
   _base.clear();
 }

 bool containsKey(Object key) => _base.containsKey(key);

 bool containsValue(Object value) => _base.containsValue(value);

 void forEach(void f(K key, V value)) {
   _base.forEach(f);
 }

 bool get isEmpty => _base.isEmpty;

 bool get isNotEmpty => _base.isNotEmpty;

 Iterable<K> get keys => _base.keys;

 int get length => _base.length;

 V putIfAbsent(K key, V ifAbsent()) => _base.putIfAbsent(key, ifAbsent);

 V remove(Object key) => _base.remove(key);

 Iterable<V> get values => _base.values;
}

Implements

Map<K, V>

Constructors

new DelegatingMap(Map<K, V> base) #

Creates a Map instance with the default implementation, LinkedHashMap.

A LinkedHashMap requires the keys to implement compatible operator== and hashCode, and it allows null as a key.

docs inherited from Map<K, V>
DelegatingMap(Map<K, V> base) : _base = base;

Properties

final bool isEmpty #

Returns true if there is no key-value pair in the map.

docs inherited from Map<K, V>
bool get isEmpty => _base.isEmpty;

final bool isNotEmpty #

Returns true if there is at least one key-value pair in the map.

docs inherited from Map<K, V>
bool get isNotEmpty => _base.isNotEmpty;

final Iterable<K> keys #

The keys of this.

The returned iterable has efficient length and contains operations, based on length and containsKey of the map.

docs inherited from Map<K, V>
Iterable<K> get keys => _base.keys;

final int length #

The number of key-value pairs in the map.

docs inherited from Map<K, V>
int get length => _base.length;

final Iterable<V> values #

The values of this.

The returned iterable has an efficient length method based on the length of the map.

docs inherited from Map<K, V>
Iterable<V> get values => _base.values;

Operators

V operator [](Object key) #

Returns the value for the given key or null if key is not in the map. Because null values are supported, one should either use containsKey to distinguish between an absent key and a null value, or use the putIfAbsent method.

docs inherited from Map<K, V>
V operator [](Object key) => _base[key];

void operator []=(K key, V value) #

Associates the key with the given value.

docs inherited from Map<K, V>
void operator []=(K key, V value) {
 _base[key] = value;
}

Methods

void addAll(Map<K, V> other) #

Adds all key-value pairs of other to this map.

If a key of other is already in this map, its value is overwritten.

The operation is equivalent to doing this[key] = value for each key and associated value in other. It iterates over other, which must therefore not change during the iteration.

docs inherited from Map<K, V>
void addAll(Map<K, V> other) {
 _base.addAll(other);
}

void clear() #

Removes all pairs from the map.

docs inherited from Map<K, V>
void clear() {
 _base.clear();
}

bool containsKey(Object key) #

Returns true if this map contains the given key.

docs inherited from Map<K, V>
bool containsKey(Object key) => _base.containsKey(key);

bool containsValue(Object value) #

Returns true if this map contains the given value.

docs inherited from Map<K, V>
bool containsValue(Object value) => _base.containsValue(value);

void forEach(void f(K key, V value)) #

Applies f to each {key, value} pair of the map.

It is an error to add or remove keys from the map during iteration.

docs inherited from Map<K, V>
void forEach(void f(K key, V value)) {
 _base.forEach(f);
}

V putIfAbsent(K key, V ifAbsent()) #

If key is not associated to a value, calls ifAbsent and updates the map by mapping key to the value returned by ifAbsent. Returns the value in the map.

Map<String, int> scores = {'Bob': 36};
for (var key in ['Bob', 'Rohan', 'Sophena']) {
  scores.putIfAbsent(key, () => key.length);
}
scores['Bob'];      // 36
scores['Rohan'];    //  5
scores['Sophena'];  //  7

The code that ifAbsent executes must not add or remove keys.

docs inherited from Map<K, V>
V putIfAbsent(K key, V ifAbsent()) => _base.putIfAbsent(key, ifAbsent);

V remove(Object key) #

Removes the association for the given key. Returns the value for key in the map or null if key is not in the map. Note that values can be null and a returned null value does not always imply that the key is absent.

docs inherited from Map<K, V>
V remove(Object key) => _base.remove(key);