actOnId method
Future<List<String> >
actOnId(
- String id,
- CrudAction action, {
- String? newValue,
- String persistKey = 'defaultPersistKey',
Performs a CRUD action on persisted data.
id: Identifier for the data item to act upon.action: Action to perform (delete or update).newValue: Optional new value for update action.persistKey: Key to use for persistence (default is 'defaultPersistKey').
Implementation
Future<List<String>> actOnId(String id, CrudAction action, {String? newValue, String persistKey = 'defaultPersistKey'}) async {
if (_socketConfig.persistStream) {
_prefs ??= await SharedPreferences.getInstance();
List<String> items = _prefs?.getStringList(persistKey) ?? [];
switch (action) {
case CrudAction.delete:
items.removeWhere((item) => item.contains(id));
break;
case CrudAction.update:
if (newValue != null) {
int index = items.indexWhere((item) => item.contains(id));
if (index != -1) {
items[index] = newValue;
}
}
break;
default:
throw ArgumentError('Invalid action: $action');
}
await _prefs?.setStringList(persistKey, items);
return items;
}
return [];
}