setSelectedKeys method
Sets the selected keys programmatically. This will try to resolve to items if data is loaded.
Implementation
void setSelectedKeys(List<K> keys) {
if (_keyExtractor == null) {
throw StateError('Cannot set selected keys without keyExtractor');
}
_selectedItems.clear();
_selectedKeys.clear();
_pendingKeys.clear();
final keysToAdd = _maxSelections != null
? keys.take(_maxSelections!)
: keys;
for (final key in keysToAdd) {
_selectedKeys.add(key);
// Try to find the item
final state = _cubit.state;
if (state is SmartPaginationLoaded<T>) {
bool found = false;
for (final item in state.items) {
if (_keyExtractor!(item) == key) {
_selectedItems.add(item);
found = true;
break;
}
}
if (!found) {
_pendingKeys.add(key);
}
} else {
_pendingKeys.add(key);
}
}
// Start listening for data if there are pending keys
if (_pendingKeys.isNotEmpty) {
_cubitSubscription ??= _cubit.stream.listen(_onCubitStateChanged);
}
_notifySelectionChanged();
notifyListeners();
}