setNested function

void setNested(
  1. Map<String, dynamic> map,
  2. List<String> path,
  3. Object? value
)

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.

Implementation

void setNested(Map<String, dynamic> map, List<String> path, Object? value) {
  if (path.isEmpty) return;
  Map<String, dynamic> current = map;
  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;
  }
  final lastKey = path.lastOrNull;
  if (lastKey != null) current[lastKey] = value;
}