flattenKeys method

  1. @useResult
Map<String, dynamic> flattenKeys({
  1. String prefix = '',
})

Flattens nested keys to dot-separated keys. prefix is prepended (for recursion).

Recurses to the map's nesting depth; not intended for untrusted, arbitrarily-deep input (deep nesting can exhaust the stack).

Implementation

@useResult
Map<String, dynamic> flattenKeys({String prefix = ''}) {
  final Map<String, dynamic> out = <String, dynamic>{};
  for (final MapEntry<String, dynamic> e in entries) {
    final String key = prefix.isEmpty ? e.key : '$prefix.${e.key}';
    final val = e.value;
    if (val is Map<String, dynamic>) {
      // ignore: saropa_lints/prefer_spread_over_addall -- accumulates across loop iterations; spread would be O(n^2)
      out.addAll(val.flattenKeys(prefix: key));
    } else {
      out[key] = val;
    }
  }
  return out;
}