UnmodifiableMapView<K, V>
LogoDart

UnmodifiableMapView<K, V>#

class UnmodifiableMapView<K, V> extends MapView<K, V> implements Map<K, V>

View of a Map that disallow modifying the map.

A wrapper around a Map that forwards all members to the map provided in the constructor, except for operations that modify the map. Modifying operations throw instead.

final baseMap = <int, String>{1: 'Mars', 2: 'Mercury', 3: 'Venus'};
final unmodifiableMapView = UnmodifiableMapView(baseMap);

// Remove an entry from the original map.
baseMap.remove(3);
print(unmodifiableMapView); // {1: Mars, 2: Mercury}

unmodifiableMapView.remove(1); // Throws.

Inheritance

Object → MapView<K, V>UnmodifiableMapView<K, V>

Implemented types

Constructors#

UnmodifiableMapView()#

UnmodifiableMapView(Map<K, V> map)
Implementation
UnmodifiableMapView(Map<K, V> map) : super(map);

Properties#

entries no setter inherited#

Iterable<MapEntry<K, V>> get entries

The map entries of this Map.

Inherited from MapView.

Implementation
Iterable<MapEntry<K, V>> get entries => _map.entries;

hashCode no setter inherited#

int get hashCode

The hash code for this object.

A hash code is a single integer which represents the state of the object that affects operator == comparisons.

All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).

If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.

Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.

Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.

If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.

Inherited from Object.

Implementation
external int get hashCode;

isEmpty no setter inherited#

bool get isEmpty

Whether there is no key/value pair in the map.

Inherited from MapView.

Implementation
bool get isEmpty => _map.isEmpty;

isNotEmpty no setter inherited#

bool get isNotEmpty

Whether there is at least one key/value pair in the map.

Inherited from MapView.

Implementation
bool get isNotEmpty => _map.isNotEmpty;

keys no setter inherited#

Iterable<K> get keys

The keys of this Map.

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

The order of iteration is defined by the individual Map implementation, but must be consistent between changes to the map.

Modifying the map while iterating the keys may break the iteration.

Inherited from MapView.

Implementation
Iterable<K> get keys => _map.keys;

length no setter inherited#

int get length

The number of key/value pairs in the map.

Inherited from MapView.

Implementation
int get length => _map.length;

runtimeType no setter inherited#

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
external Type get runtimeType;

values no setter inherited#

Iterable<V> get values

The values of this Map.

The values are iterated in the order of their corresponding keys. This means that iterating keys and values in parallel will provide matching pairs of keys and values.

The returned iterable has an efficient length method based on the length of the map. Its Iterable.contains method is based on == comparison.

Modifying the map while iterating the values may break the iteration.

Inherited from MapView.

Implementation
Iterable<V> get values => _map.values;

Methods#

addAll() inherited#

void addAll(Map<K, V> other)

This operation is not supported by an unmodifiable map.

Inherited from _UnmodifiableMapMixin.

Implementation
void addAll(Map<K, V> other) {
  throw UnsupportedError("Cannot modify unmodifiable map");
}

addEntries() inherited#

void addEntries(Iterable<MapEntry<K, V>> entries)

This operation is not supported by an unmodifiable map.

Inherited from _UnmodifiableMapMixin.

Implementation
void addEntries(Iterable<MapEntry<K, V>> entries) {
  throw UnsupportedError("Cannot modify unmodifiable map");
}

cast() override#

Map<RK, RV> cast<RK, RV>()

Provides a view of this map as having RK keys and RV instances, if necessary.

If this map is already a Map<RK, RV>, it is returned unchanged.

If this set contains only keys of type RK and values of type RV, all read operations will work correctly. If any operation exposes a non-RK key or non-RV value, the operation will throw instead.

Entries added to the map must be valid for both a Map<K, V> and a Map<RK, RV>.

Methods which accept Object? as argument, like containsKey, remove and operator [], will pass the argument directly to the this map's method without any checks. That means that you can do mapWithStringKeys.cast<int,int>().remove("a") successfully, even if it looks like it shouldn't have any effect.

Implementation
Map<RK, RV> cast<RK, RV>() =>
    UnmodifiableMapView<RK, RV>(_map.cast<RK, RV>());

clear() inherited#

void clear()

This operation is not supported by an unmodifiable map.

Inherited from _UnmodifiableMapMixin.

Implementation
void clear() {
  throw UnsupportedError("Cannot modify unmodifiable map");
}

containsKey() inherited#

bool containsKey(Object? key)

Whether this map contains the given key.

Returns true if any of the keys in the map are equal to key according to the equality used by the map.

final moonCount = <String, int>{'Mercury': 0, 'Venus': 0, 'Earth': 1,
  'Mars': 2, 'Jupiter': 79, 'Saturn': 82, 'Uranus': 27, 'Neptune': 14};
final containsUranus = moonCount.containsKey('Uranus'); // true
final containsPluto = moonCount.containsKey('Pluto'); // false

Inherited from MapView.

Implementation
bool containsKey(Object? key) => _map.containsKey(key);

containsValue() inherited#

bool containsValue(Object? value)

Whether this map contains the given value.

Returns true if any of the values in the map are equal to value according to the == operator.

final moonCount = <String, int>{'Mercury': 0, 'Venus': 0, 'Earth': 1,
  'Mars': 2, 'Jupiter': 79, 'Saturn': 82, 'Uranus': 27, 'Neptune': 14};
final moons3 = moonCount.containsValue(3); // false
final moons82 = moonCount.containsValue(82); // true

Inherited from MapView.

Implementation
bool containsValue(Object? value) => _map.containsValue(value);

forEach() inherited#

void forEach(void Function(K key, V value) action)

Applies action to each key/value pair of the map.

Calling action must not add or remove keys from the map.

final planetsByMass = <num, String>{0.81: 'Venus', 1: 'Earth',
  0.11: 'Mars', 17.15: 'Neptune'};

planetsByMass.forEach((key, value) {
  print('$key: $value');
  // 0.81: Venus
  // 1: Earth
  // 0.11: Mars
  // 17.15: Neptune
});

Inherited from MapView.

Implementation
void forEach(void action(K key, V value)) {
  _map.forEach(action);
}

map() inherited#

Map<K2, V2> map<K2, V2>( MapEntry<K2, V2> Function(K key, V value) transform, );

Returns a new map where all entries of this map are transformed by the given convert function.

Inherited from MapView.

Implementation
Map<K2, V2> map<K2, V2>(MapEntry<K2, V2> transform(K key, V value)) =>
    _map.map<K2, V2>(transform);

noSuchMethod() inherited#

dynamic noSuchMethod(Invocation invocation)

Invoked when a nonexistent method or property is accessed.

A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:

dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).

Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.

A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:

class MockList<T> implements List<T> {
  noSuchMethod(Invocation invocation) {
    log(invocation);
    super.noSuchMethod(invocation); // Will throw.
  }
}
void main() {
  MockList().add(42);
}

This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.

If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.

The default behavior is to throw a NoSuchMethodError.

Inherited from Object.

Implementation
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);

putIfAbsent() inherited#

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

This operation is not supported by an unmodifiable map.

Inherited from _UnmodifiableMapMixin.

Implementation
V putIfAbsent(K key, V ifAbsent()) {
  throw UnsupportedError("Cannot modify unmodifiable map");
}

remove() inherited#

V? remove(Object? key)

This operation is not supported by an unmodifiable map.

Inherited from _UnmodifiableMapMixin.

Implementation
V? remove(Object? key) {
  throw UnsupportedError("Cannot modify unmodifiable map");
}

removeWhere() inherited#

void removeWhere(bool Function(K key, V value) test)

This operation is not supported by an unmodifiable map.

Inherited from _UnmodifiableMapMixin.

Implementation
void removeWhere(bool test(K key, V value)) {
  throw UnsupportedError("Cannot modify unmodifiable map");
}

toString() inherited#

String toString()

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Inherited from MapView.

Implementation
String toString() => _map.toString();

update() inherited#

V update( K key, V Function(V value) update, { V Function()? ifAbsent, });

This operation is not supported by an unmodifiable map.

Inherited from _UnmodifiableMapMixin.

Implementation
V update(K key, V update(V value), {V Function()? ifAbsent}) {
  throw UnsupportedError("Cannot modify unmodifiable map");
}

updateAll() inherited#

void updateAll(V Function(K key, V value) update)

This operation is not supported by an unmodifiable map.

Inherited from _UnmodifiableMapMixin.

Implementation
void updateAll(V update(K key, V value)) {
  throw UnsupportedError("Cannot modify unmodifiable map");
}

Operators#

operator ==() inherited#

bool operator ==(Object other)

The equality operator.

The default behavior for all Objects is to return true if and only if this object and other are the same object.

Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:

  • Total: It must return a boolean for all arguments. It should never throw.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must be true.

The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.

If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.

Inherited from Object.

Implementation
external bool operator ==(Object other);

operator []() inherited#

V? operator [](Object? key)

The value for the given key, or null if key is not in the map.

Some maps allow null as a value. For those maps, a lookup using this operator cannot distinguish between a key not being in the map, and the key being there with a null value. Methods like containsKey or putIfAbsent can be used if the distinction is important.

Inherited from MapView.

Implementation
V? operator [](Object? key) => _map[key];

operator []=() inherited#

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

This operation is not supported by an unmodifiable map.

Inherited from _UnmodifiableMapMixin.

Implementation
void operator []=(K key, V value) {
  throw UnsupportedError("Cannot modify unmodifiable map");
}