collectGarbage method

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

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.

Implementation

@override
void collectGarbage(int leadingGarbage, int trailingGarbage) {
  int count = leadingGarbage;
  RenderBox? child = firstChild!;
  while (count > 0 && child != null) {
    final crossAxisIndex = _childCrossAxisIndex(child);
    if (crossAxisIndex != null) {
      _previousCrossAxisIndexes.add(crossAxisIndex);
      _previousMainAxisExtents.add(paintExtentOf(child));
    }
    child = childAfter(child);
    count -= 1;
  }
  super.collectGarbage(leadingGarbage, trailingGarbage);
}