addByKey method

void addByKey(
  1. K key
)

Adds an item by its key. If the item is already loaded, it will be added immediately. Otherwise, the key will be stored as pending.

Implementation

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

  if (isMaxSelectionsReached) return;
  if (_selectedKeys.contains(key)) return;

  // 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) {
        addItem(item);
        return;
      }
    }
  }

  // Item not found - add key and mark as pending
  _selectedKeys.add(key);
  _pendingKeys.add(key);

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

  _notifySelectionChanged();
  notifyListeners();
}