insert method

void insert(
  1. NodeV0 entry, {
  2. int? index,
})

Implementation

void insert(NodeV0 entry, {int? index}) {
  final length = children.length;
  index ??= length;

  if (children.isEmpty) {
    entry.parent = this;
    children.add(entry);
    notifyListeners();
    return;
  }

  // If index is out of range, insert at the end.
  // If index is negative, insert at the beginning.
  // If index is positive, insert at the index.
  if (index >= length) {
    children.last.insertAfter(entry);
  } else if (index <= 0) {
    children.first.insertBefore(entry);
  } else {
    childAtIndex(index)?.insertBefore(entry);
  }
}