clearSublist method

List<T> clearSublist([
  1. String sublistName = 'checks'
])

Returns a new list where the specified sublist in each entry is cleared.

This method iterates through the list and, for each entry containing the specified sublist, replaces it with an empty list. By default, it clears the sublist named 'checks', but a different sublist can be specified using sublistName.

The original list remains unchanged, and a new modified list is returned.

Example:

List<Map<String, dynamic>> tasks = [
  {'name': 'Task 1', 'checks': [true, false, true]},
  {'name': 'Task 2', 'checks': [false, false]}
];

List<Map<String, dynamic>> updatedTasks = tasks.clearSublist();

print(updatedTasks);
// Output:
// [
//   {'name': 'Task 1', 'checks': []},
//   {'name': 'Task 2', 'checks': []}
// ]
  • sublistName (optional) The key name of the sublist to clear (default is 'checks').

Returns a new list with the specified sublists cleared.

Implementation

List<T> clearSublist([String sublistName = 'checks']) {
  List<T> parent = this.toList();

  for (dynamic entry in parent) {
    if (entry.containsKey(sublistName)) {
      entry[sublistName] = [];
    }
  }

  return parent;
}