get<T> function

T? get<T>(
  1. dynamic map,
  2. String path, {
  3. T converter(
    1. dynamic
    )?,
})

Get value from a Map by path

Use dot notation in path to access nessted keys

Use convertor to cast the valeu to your custom type

Returns T?

Map map = {'a': {'b': 1}};
get(map, 'a.b');

Implementation

T? get<T>(map, String path, {T Function(dynamic)? converter}) {
  List<String> keys = path.split('.');

  if (map[keys[0]] == null) {
    return null;
  }

  if (keys.length == 1) {
    return converter != null ? converter(map[keys[0]]) : map[keys[0]] as T;
  }

  return get(map[keys.removeAt(0)], keys.join('.'));
}