collectGarbage method

  1. @protected
void collectGarbage(
  1. int leadingGarbage,
  2. int trailingGarbage
)

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

Children whose SliverMultiBoxAdaptorParentData.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.

See also:

Implementation

@protected
void collectGarbage(int leadingGarbage, int trailingGarbage) {
  assert(_debugAssertChildListLocked());
  assert(childCount >= leadingGarbage + trailingGarbage);
  invokeLayoutCallback<SliverConstraints>((SliverConstraints constraints) {
    while (leadingGarbage > 0) {
      _destroyOrCacheChild(firstChild!);
      leadingGarbage -= 1;
    }
    while (trailingGarbage > 0) {
      _destroyOrCacheChild(lastChild!);
      trailingGarbage -= 1;
    }
    // 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 SliverMultiBoxAdaptorParentData childParentData = child.parentData! as SliverMultiBoxAdaptorParentData;
      return !childParentData.keepAlive;
    }).toList().forEach(_childManager.removeChild);
    assert(_keepAliveBucket.values.where((RenderBox child) {
      final SliverMultiBoxAdaptorParentData childParentData = child.parentData! as SliverMultiBoxAdaptorParentData;
      return !childParentData.keepAlive;
    }).isEmpty);
  });
}