getMap<K, V> method

Map<K, V> getMap<K, V>()

Reads constantReader and returns an object of type Map<K, V>.

Throws if an instance of Map<K, V> cannot be constructed.

Implementation

Map<K, V> getMap<K, V>() {
  if (!holdsA<Map<K, V>>()) {
    throw ErrorOf<ConstantReader>(
        message: 'Input does not represent an object of type Map<$K, $V>.',
        invalidState: 'Input represents an object of type '
            '$dartType.');
  }
  if (!_decoders.containsKey(K)) {
    throw ErrorOf<ConstantReader>(
        message: 'Could not read map key of type [$K].',
        invalidState: 'A decoder function for type [$K] is missing.',
        expectedState: 'Use addDecoder<$K>() to register '
            'a decoder function for type [$K].');
  }
  if (!_decoders.containsKey(V) && V != dynamic) {
    throw ErrorOf<ConstantReader>(
        message: 'Could not read map value of type [$V].',
        invalidState: 'A decoder function for type [$V] is missing.',
        expectedState: 'Use addDecoder<$V>() to register a '
            'decoder function for type [$V].');
  }
  return mapValue.map((keyObj, valueObj) {
    final key = ConstantReader(keyObj).get<K>();
    final value = ConstantReader(valueObj).get<V>();
    return MapEntry<K, V>(key, value);
  });
}