selectedItem property

int get selectedItem

The currently selected item index that's closest to the center of the viewport.

There are circumstances that this FixedExtentScrollController can't know the current item. Reading selectedItem will throw an AssertionError in the following cases:

  1. No scroll view is currently using this FixedExtentScrollController.
  2. More than one scroll views using the same FixedExtentScrollController.

The hasClients property can be used to check if a scroll view is attached prior to accessing selectedItem.

Implementation

int get selectedItem {
  assert(
    positions.isNotEmpty,
    'FixedExtentScrollController.selectedItem cannot be accessed before a '
    'scroll view is built with it.',
  );
  assert(
    positions.length == 1,
    'The selectedItem property cannot be read when multiple scroll views are '
    'attached to the same FixedExtentScrollController.',
  );
  final _FixedExtentScrollPosition position = this.position as _FixedExtentScrollPosition;
  return position.itemIndex;
}