selectByKey method

void selectByKey(
  1. K key
)

Selects an item by its key. If the item is already loaded, it will be selected immediately. Otherwise, the key will be stored as pending and resolved when data loads.

Implementation

void selectByKey(K key) {
  if (_keyExtractor == null) {
    throw StateError('Cannot select by key without keyExtractor');
  }

  // Try to find the item in current data
  final state = _cubit.state;
  if (state is SmartPaginationLoaded<T>) {
    for (final item in state.items) {
      if (_keyExtractor!(item) == key) {
        selectItem(item);
        return;
      }
    }
  }

  // Item not found - store as pending
  _selectedKey = key;
  _pendingKey = key;
  _selectedItem = null;

  // Start listening for data if not already
  _cubitSubscription ??= _cubit.stream.listen(_onCubitStateChanged);

  _onKeySelected?.call(key);
  notifyListeners();
}