setNested function
Sets value at the nested location addressed by path in map, creating
intermediate maps as needed. Does nothing if path is empty, or stops early
if an existing intermediate value along path is not a map.
Audited: 2026-06-12 11:26 EDT
Implementation
void setNested(Map<String, dynamic> map, List<String> path, Object? value) {
if (path.isEmpty) return;
Map<String, dynamic> current = map;
bool descended = true;
for (int i = 0; i < path.length - 1; i++) {
final String key = path[i];
final next = current.putIfAbsent(key, () => <String, dynamic>{});
if (next is Map<String, dynamic>) {
current = next;
} else {
// An existing intermediate is not a map: actually stop (the old code kept
// `current` at the parent and still wrote lastKey there, silently
// misplacing the value at the wrong level).
descended = false;
break;
}
}
final lastKey = path.lastOrNull;
if (descended && lastKey != null) current[lastKey] = value;
}