removeItem method Null safety

void removeItem(
  1. int index,
  2. AnimatedListRemovedItemBuilder builder,
  3. {Duration duration = _kDuration}
)

Remove the item at index and start an animation that will be passed to builder when the item is visible.

Items are removed immediately. After an item has been removed, its index will no longer be passed to the SliverAnimatedList.itemBuilder. However the item will still appear in the list for duration and during that time builder must construct its widget as needed.

This method's semantics are the same as Dart's List.remove method: it decreases the length of the list by one and shifts all items at or before index towards the beginning of the list.

Implementation

void removeItem(int index, AnimatedListRemovedItemBuilder builder, { Duration duration = _kDuration }) {
  assert(index != null && index >= 0);
  assert(builder != null);
  assert(duration != null);

  final int itemIndex = _indexToItemIndex(index);
  assert(itemIndex >= 0 && itemIndex < _itemsCount);
  assert(_activeItemAt(_outgoingItems, itemIndex) == null);

  final _ActiveItem? incomingItem = _removeActiveItemAt(_incomingItems, itemIndex);
  final AnimationController controller = incomingItem?.controller
    ?? AnimationController(duration: duration, value: 1.0, vsync: this);
  final _ActiveItem outgoingItem = _ActiveItem.outgoing(controller, itemIndex, builder);
  setState(() {
    _outgoingItems
      ..add(outgoingItem)
      ..sort();
  });

  controller.reverse().then<void>((void value) {
    _removeActiveItemAt(_outgoingItems, outgoingItem.itemIndex)!.controller!.dispose();

    // Decrement the incoming and outgoing item indices to account
    // for the removal.
    for (final _ActiveItem item in _incomingItems) {
      if (item.itemIndex > outgoingItem.itemIndex) {
        item.itemIndex -= 1;
      }
    }
    for (final _ActiveItem item in _outgoingItems) {
      if (item.itemIndex > outgoingItem.itemIndex) {
        item.itemIndex -= 1;
      }
    }

    setState(() => _itemsCount -= 1);
  });
}