forget static method
Removes key (dot notation) from map. Returns the map for chaining.
When the leaf lives inside a List, the element is removed via
removeAt, shifting subsequent indices.
Implementation
static Map forget(Map map, String key) {
if (key.isEmpty) return map;
if (map.containsKey(key)) {
map.remove(key);
return map;
}
final segments = key.split('.');
dynamic current = map;
for (var i = 0; i < segments.length - 1; i++) {
final s = segments[i];
if (current is Map) {
final next = current[s];
if (next is Map || next is List) {
current = next;
} else {
return map;
}
} else if (current is List) {
final idx = int.tryParse(s);
if (idx == null || idx < 0 || idx >= current.length) return map;
current = current[idx];
} else {
return map;
}
}
final last = segments.last;
if (current is Map) {
current.remove(last);
} else if (current is List) {
final idx = int.tryParse(last);
if (idx != null && idx >= 0 && idx < current.length) {
current.removeAt(idx);
}
}
return map;
}