upcastToMap<K, V> method
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
isnull
, an empty map is returned. - If
source
is aMap
, it is directly converted usingMap<K, V>.from
. - If
source
is aHashMap
, 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);