getSelectedMany<T> method

List<T> getSelectedMany<T>(
  1. List<T> items, {
  2. int? fallbackIndex,
})

Gets the selected items from a list.

Returns items at selected indices, sorted by index. If nothing is selected and fallbackIndex is provided, returns a list with just that item.

Implementation

List<T> getSelectedMany<T>(List<T> items, {int? fallbackIndex}) {
  if (_selected.isEmpty) {
    if (fallbackIndex != null &&
        fallbackIndex >= 0 &&
        fallbackIndex < items.length) {
      return [items[fallbackIndex]];
    }
    return [];
  }

  final indices = _selected.toList()..sort();
  return indices
      .where((i) => i >= 0 && i < items.length)
      .map((i) => items[i])
      .toList();
}