upcastToMap<K, V> method

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

A TypeAdapter that adapts a raw Object into a strongly typed Map of type K, V.

Jetleaf’s conversion service may return Map<Object, Object> or HashMap, which may represent key-value pairs. This adapter ensures that the raw input is converted into a Dart Map<K, V>.

  • If source is null, an empty map is returned.
  • If source is a Map, it is directly converted using Map<K, V>.from.
  • If source is a HashMap, entries are iterated and retyped.
  • Otherwise, an IllegalArgumentException is thrown.

Example

final adapter = MapAdapter<String, int>();
final map = adapter.adapt({'a': 1, 'b': 2}); // {a: 1, b: 2}

final rawHashMap = HashMap()
  ..['x'] = 10
  ..['y'] = 20;
final adapted = adapter.adapt(rawHashMap); // {x: 10, y: 20}

final empty = adapter.adapt(null); // {}

Implementation

Map<K, V> upcastToMap<K, V>() => MapAdapter<K, V>().adapt(this);