ListView.separated constructor Null safety

ListView.separated(
  1. {Key? key,
  2. Axis scrollDirection = Axis.vertical,
  3. bool reverse = false,
  4. ScrollController? controller,
  5. bool? primary,
  6. ScrollPhysics? physics,
  7. bool shrinkWrap = false,
  8. EdgeInsetsGeometry? padding,
  9. required IndexedWidgetBuilder itemBuilder,
  10. ChildIndexGetter? findChildIndexCallback,
  11. required IndexedWidgetBuilder separatorBuilder,
  12. required int itemCount,
  13. bool addAutomaticKeepAlives = true,
  14. bool addRepaintBoundaries = true,
  15. bool addSemanticIndexes = true,
  16. double? cacheExtent,
  17. DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  18. ScrollViewKeyboardDismissBehavior keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual,
  19. String? restorationId,
  20. Clip clipBehavior = Clip.hardEdge}
)

Creates a fixed-length scrollable linear array of list "items" separated by list item "separators".

This constructor is appropriate for list views with a large number of item and separator children because the builders are only called for the children that are actually visible.

The itemBuilder callback will be called with indices greater than or equal to zero and less than itemCount.

Separators only appear between list items: separator 0 appears after item 0 and the last separator appears before the last item.

The separatorBuilder callback will be called with indices greater than or equal to zero and less than itemCount - 1.

The itemBuilder and separatorBuilder callbacks should always return a non-null widget, and actually create widget instances when called. Avoid using a builder that returns a previously-constructed widget; if the list view's children are created in advance, or all at once when the ListView itself is created, it is more efficient to use the ListView constructor.

The findChildIndexCallback corresponds to the SliverChildBuilderDelegate.findChildIndexCallback property. If null, a child widget may not map to its existing RenderObject when the order of children returned from the children builder changes. This may result in state-loss. This callback needs to be implemented if the order of the children may change at a later time.

This example shows how to create ListView whose ListTile list items are separated by Dividers.
ListView.separated(
  itemCount: 25,
  separatorBuilder: (BuildContext context, int index) => const Divider(),
  itemBuilder: (BuildContext context, int index) {
    return ListTile(
      title: Text('item $index'),
    );
  },
)

The addAutomaticKeepAlives argument corresponds to the SliverChildBuilderDelegate.addAutomaticKeepAlives property. The addRepaintBoundaries argument corresponds to the SliverChildBuilderDelegate.addRepaintBoundaries property. The addSemanticIndexes argument corresponds to the SliverChildBuilderDelegate.addSemanticIndexes property. None may be null.

Implementation

ListView.separated({
  super.key,
  super.scrollDirection,
  super.reverse,
  super.controller,
  super.primary,
  super.physics,
  super.shrinkWrap,
  super.padding,
  required IndexedWidgetBuilder itemBuilder,
  ChildIndexGetter? findChildIndexCallback,
  required IndexedWidgetBuilder separatorBuilder,
  required int itemCount,
  bool addAutomaticKeepAlives = true,
  bool addRepaintBoundaries = true,
  bool addSemanticIndexes = true,
  super.cacheExtent,
  super.dragStartBehavior,
  super.keyboardDismissBehavior,
  super.restorationId,
  super.clipBehavior,
}) : assert(itemBuilder != null),
     assert(separatorBuilder != null),
     assert(itemCount != null && itemCount >= 0),
     itemExtent = null,
     prototypeItem = null,
     childrenDelegate = SliverChildBuilderDelegate(
       (BuildContext context, int index) {
         final int itemIndex = index ~/ 2;
         final Widget widget;
         if (index.isEven) {
           widget = itemBuilder(context, itemIndex);
         } else {
           widget = separatorBuilder(context, itemIndex);
           assert(() {
             if (widget == null) {
               throw FlutterError('separatorBuilder cannot return null.');
             }
             return true;
           }());
         }
         return widget;
       },
       findChildIndexCallback: findChildIndexCallback,
       childCount: _computeActualChildCount(itemCount),
       addAutomaticKeepAlives: addAutomaticKeepAlives,
       addRepaintBoundaries: addRepaintBoundaries,
       addSemanticIndexes: addSemanticIndexes,
       semanticIndexCallback: (Widget _, int index) {
         return index.isEven ? index ~/ 2 : null;
       },
     ),
     super(
       semanticChildCount: itemCount,
     );