setItems method

void setItems(
  1. List<T> items
)

Sets the items in this collection.

Useful for when the this controller's items are supposed to be mirroring another collection, but that other collection was changed in a way that insert/remove couldn't be called.

Implementation

void setItems(List<T> items) {
  // If there is a timer of some sort being used right now, either for
  // expanding/collapsing, or when multiple items are being added, we won't
  // set the items, as that could mess up what the timer is trying to
  // accomplish.
  // This will also only perform if the controller has been initialized.
  if (!(_timer?.isActive ?? false) && _initialized) {
    int numItemsDifference = _items.length - items.length;

    _items = List.from(items);

    // if the list is currently expanded, then we'll need to update the list's
    // state
    if (!isCollapsed()) {
      // if the difference is negative, then we'll need to insert that many
      // items into the list state
      if (numItemsDifference < 0) {
        int numItemsToAdd = -1 * numItemsDifference;
        for (int i = 0; i < numItemsToAdd; i++) {
          // index doesn't matter, as we just want the state's internal list
          // count to change
          listKey.currentState?.insertItem(0, duration: const Duration(seconds: 0));
        }
      }

      // otherwise, if positive, we'll need to remove items
      else if (numItemsDifference > 0) {
        for (int i = 0; i < numItemsDifference; i++) {
          // index doesn't matter, as we just want the state's internal list
          // count to change
          listKey.currentState?.removeItem(
            0,
            (context, animation) => Container(),
            duration: const Duration(seconds: 0),
          );
        }
      }
    }

    _numItemsDisplayed =
        value == ExpandableSliverListStatus.collapsed ? 0 : _items.length;

    _calcItemPeriod();
  }
}