findInMap function

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

can dig deeper into the Map if the key is not nested return the value if the key is not nested go deep until find the value

Implementation

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

  /// split and remove empty
  final keys = path.split(splitOperator);

  /// remove empty keys
  keys.removeWhere((e) => e.isEmpty);

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