flattenMap function
Recursively flattens nested maps in input into dotted keys.
Nested maps become parent<separator>child keys; nested lists become
indexed keys (tags.0, tags.1, ...). Scalar leaves are kept as-is. The
separator defaults to . and joins each level of nesting.
Example:
flattenMap(<String, Object?>{
'user': <String, Object?>{'name': 'Ann'},
'tags': <Object?>['a', 'b'],
});
// {'user.name': 'Ann', 'tags.0': 'a', 'tags.1': 'b'}
Audited: 2026-06-12 11:26 EDT
Implementation
Map<String, Object?> flattenMap(
Map<String, Object?> input, {
String separator = '.',
}) {
final Map<String, Object?> out = <String, Object?>{};
input.forEach((String key, Object? value) {
_flattenInto(out, key, value, separator);
});
return out;
}