collectGarbage method

  1. @protected
void collectGarbage(
  1. Set<int> visibleIndices
)

Called after layout with the number of children that can be garbage collected at the head and tail of the child list.

Children whose SliverVariableSizeBoxAdaptorParentData.keepAlive property is set to true will be removed to a cache instead of being dropped.

This method also collects any children that were previously kept alive but are now no longer necessary. As such, it should be called every time performLayout is run, even if the arguments are both zero.

Implementation

@protected
void collectGarbage(Set<int> visibleIndices) {
  assert(_debugAssertChildListLocked());
  assert(childCount >= visibleIndices.length);
  invokeLayoutCallback<SliverConstraints>((SliverConstraints constraints) {
    // We destroy only those which are not visible.
    indices.toSet().difference(visibleIndices).forEach(_destroyOrCacheChild);

    // Ask the child manager to remove the children that are no longer being
    // kept alive. (This should cause _keepAliveBucket to change, so we have
    // to prepare our list ahead of time.)
    _keepAliveBucket.values
        .where((RenderBox child) {
          final SliverVariableSizeBoxAdaptorParentData childParentData =
              child.parentData as SliverVariableSizeBoxAdaptorParentData;
          return !childParentData.keepAlive;
        })
        .toList()
        .forEach(_childManager.removeChild);
    assert(_keepAliveBucket.values.where((RenderBox child) {
      final SliverVariableSizeBoxAdaptorParentData childParentData =
          child.parentData as SliverVariableSizeBoxAdaptorParentData;
      return !childParentData.keepAlive;
    }).isEmpty);
  });
}