findInMap function

Object? findInMap(
  1. String path,
  2. Map map, {
  3. String splitOperator = '.',
})

goes into the map extract the value by the key

Implementation

Object? findInMap(
  String path,
  Map map, {
  String splitOperator = '.',
}) {
  /// extract if no parent
  if (!path.contains('.')) return map[path];

  /// split and remove empty
  final keys = path.split('.')..removeWhere((e) => e.isEmpty);

  /// if the key is nested
  if (keys.isNotEmpty) {
    if (keys.length == 1) return map[keys.first];
    final firstKey = keys.first;
    final value = map[firstKey];
    if (value is Map) {
      final newKey = path.replaceFirst('$firstKey.', '');
      return findInMap(
        newKey,
        value,
        splitOperator: splitOperator,
      );
    }
  }
}