updateItemInInfiniteQuery<T> method
void
updateItemInInfiniteQuery<T>({
- required String keyPrefix,
- required dynamic matcher(
- T
- required dynamic updater(
- T
Find by the matcher
and replace with the updater
in all query cache results, that start with the
keyPrefix
, and are of type DAQInfiniteQueryResponse
Implementation
void updateItemInInfiniteQuery<T>({
required String keyPrefix,
required Function(T) matcher,
required Function(T) updater,
}) {
final mutatedKeys = <String>[];
final mutatedData = <String, dynamic>{};
final allKeys = getKeysByPattern('${keyPrefix}_*');
for (String key in allKeys) {
final singleResponse = getValue(key);
if (singleResponse is DAQInfiniteQueryResponse<T>?) {
if (singleResponse == null) continue;
final updatedItems = singleResponse.items.map<T>((item) {
if (matcher(item)) {
return updater(item);
}
return item;
}).toList();
final updatedResponse = singleResponse.copyWith(items: updatedItems);
addToCache(key, updatedResponse);
mutatedKeys.add(key);
mutatedData[key] = updatedResponse;
}
}
if (mutatedKeys.isNotEmpty) {
updateCacheBatch(mutatedKeys, mutatedData);
}
}