removeChild function

Future<void> removeChild(
  1. WidgetModel model,
  2. List arguments
)

Implementation

Future<void> removeChild(WidgetModel model, List<dynamic> arguments) async {
  // if index is null, remove all children before replacement.
  int? index = toInt(elementAt(arguments, 0));

  // check for children then remove them
  if (model.children != null && index == null) {
    // dispose of the last item
    model.children!.last.dispose();

    // check if the list is greater than 0, remove at the final index.
    if (model.children!.isNotEmpty) model.children!.removeLast();
  } else if (model.children != null && index != null) {
    // check if index is in range, then dispose of the child at that index.
    if (index >= 0 && model.children!.length > index) {
      model.children![index].dispose();
      model.children!.removeAt(index);
    }
    // Could add handling for negative index removing from the end?
  }
}