Invalidation & Manual Updates
When state changes on the server (e.g. through a mutation or a server push event), you need to synchronize your client cache. fquery_core provides two strategies:
- Invalidation (
invalidateQueries): Mark queries as dirty and refetch them from the source of truth. - Manual Updates (
setQueryData): Update cached records directly in memory for instantaneous zero-latency updates.
Query Invalidation
dart
// 1. Invalidate a specific item
cache.invalidateQueries(['posts', 42], exact: true);
// 2. Invalidate an entire group by prefix
cache.invalidateQueries(['posts']);
// 3. Invalidate with parameter matching
cache.invalidateQueries(['posts', {'status': 'published'}]);What Happens on Invalidation?
- If an observer is active (mounted and enabled), it immediately initiates a background fetch and notifies subscribers when complete.
- If no observer is currently mounted, the query state is flagged with
isInvalidated = true. When an observer subsequently mounts, it bypassesstaleDurationand fetches immediately.
Manual Updates (setQueryData)
If the server response already includes the updated object, you can directly write it to the cache to avoid a redundant HTTP GET request:
dart
// Direct replacement or functional update
cache.setQueryData<Post, Exception>(['posts', 42], (previous) {
if (previous == null) return newPost;
return previous.copyWith(title: 'Updated Title');
});Removing Queries (removeQueries)
If you want to clear queries completely:
dart
// Remove exact query
cache.removeQueries(['posts', 42], exact: true);
// Remove all queries matching prefix
cache.removeQueries(['posts']);- If active observers are subscribed,
removeQueriesperforms a hard reset: data is reset tonulland a fresh fetch begins. - If no observers are subscribed, the query is deleted from memory.