readMap<K, V> method

Map<K, V> readMap<K, V>(
  1. K readKey(),
  2. V readValue()
)

Decodes a map with length prefix

Format: u32 length + (key, value) pairs

Example:

final map = decoder.readMap<String, int>(
  () => decoder.readString(),
  () => decoder.readU32(),
);

Implementation

Map<K, V> readMap<K, V>(K Function() readKey, V Function() readValue) {
  final length = readU32();
  final map = <K, V>{};

  for (int i = 0; i < length; i++) {
    final key = readKey();
    final value = readValue();
    map[key] = value;
  }

  return map;
}