explode function
Explodes row into one row per element of the list at arrayKey.
Each result row copies the other fields of row and replaces arrayKey
with a single element. An empty list yields an empty result; a missing key
or a non-list value yields the single original row unchanged.
Example:
explode(<String, Object?>{'id': 1, 'tags': <Object?>['a', 'b']}, 'tags');
// [{'id': 1, 'tags': 'a'}, {'id': 1, 'tags': 'b'}]
Audited: 2026-06-12 11:26 EDT
Implementation
List<Map<String, Object?>> explode(Map<String, Object?> row, String arrayKey) {
final Object? value = row[arrayKey];
// Missing key or non-list value: nothing to fan out, return the row as-is.
if (value is! List<Object?>) {
return <Map<String, Object?>>[Map<String, Object?>.of(row)];
}
// Emit one row per element, each carrying that element under arrayKey.
return <Map<String, Object?>>[
for (final Object? element in value) <String, Object?>{...row, arrayKey: element},
];
}