remove static method
dynamic
remove(
- dynamic input,
- String path
)
Implementation
static dynamic remove(dynamic input, String path) {
if (path.isEmpty) return input;
final parts = path.split('.');
if (parts.isEmpty) return input;
final key = parts.first;
final restPath = parts.sublist(1).join('.');
if (input == null) return input;
// Handle Map case
if (input is Map<dynamic, dynamic>) {
if (!input.containsKey(key)) return input;
if (restPath.isEmpty) {
final mapCopy = Map<dynamic, dynamic>.from(input);
mapCopy.remove(key);
return mapCopy;
}
// Recursively remove from nested structure
final mapCopy = Map<dynamic, dynamic>.from(input);
mapCopy[key] = remove(mapCopy[key], restPath);
return mapCopy;
}
// Handle List case
final index = int.tryParse(key);
if (input is List && index != null && index >= 0 && index < input.length) {
if (restPath.isEmpty) {
final listCopy = List.from(input);
listCopy.removeAt(index);
return listCopy;
}
// Recursively remove from nested structure
final listCopy = List.from(input);
listCopy[index] = remove(listCopy[index], restPath);
return listCopy;
}
// If none of the above, return input unchanged
return input;
}