Reading and updating the cache
With the QueryClient, you update what every screen shows without a new request, refetch data after a change on the server, and fetch before a screen opens. In a widget, context.queryClient returns the client the widgets use. Elsewhere, use the client you configured: Fuery.client, or the one you passed to FueryProvider. See Which client a query uses.
Reading and writing the cache
Section titled “Reading and writing the cache”The client keeps one cache entry (CachedQuery) for each query key: the data and state of that key. Read and write the data with the query definition:
final client = Fuery.client;
client.getData(todosQuery); // the data, or nullclient.setData(todoQuery(1), todo);client.updateData(todosQuery, (todos) => [...?todos, todo]);client.getQueryState(['todos'])?.dataUpdatedAt; // the whole QueryStategetData,setData, andupdateDatatake the key and the data type from the query definition, so there is nothing to cast.- Every widget that uses the key rebuilds with the new data.
- When the key has no cache entry,
setDatacreates one with every option of the query, so Fuery stores its data withpersistand can refetch it. - Returning
nullfrom theupdateDataupdater leaves the cache unchanged.
With only a key, use getQueryData, setQueryData, and updateQueryData, and name the data type: client.getQueryData<List<Todo>>(['todos']). A cache entry that setQueryData creates has no query function, so refetches skip it until an observer or client.query brings a query for its key. Reading a key as another data type throws a StateError.
getQueryState returns the QueryState of one key, such as when its data was last updated. QueryState fields lists its fields.
To write many keys at once, for example from one websocket frame, wrap the writes in notifyManager.batch. Fuery then notifies the widgets once, after the last write:
notifyManager.batch(() { for (final todo in frame.todos) { client.setQueryData(['todo', todo.id], todo); }});Updating many queries at once
Section titled “Updating many queries at once”updateQueriesData updates every cache entry under a key whose data has the updater’s type, such as a post in every cached search result:
client.updateQueriesData( queryKey: ['posts', 'search'], (List<Post> posts) => [ for (final post in posts) post.id == id ? post.copyWith(liked: true) : post, ],);- Fuery skips cache entries of another data type under the key, so one prefix can hold lists and details.
- Fuery skips cache entries without data. Returning
nullleaves a cache entry unchanged. - Give the updater’s parameter the exact data type of the cache entries, not a supertype such as
Iterable<Post>. Without a type,updateQueriesDatathrows anArgumentError. updatedAtsets when the new data counts as fetched, as forsetData.
Listing what is cached
Section titled “Listing what is cached”getQueriesData returns the key and data of every cache entry that matches queryKey, exact, and predicate:
for (final (key, todo) in client.getQueriesData<Todo>(queryKey: ['todo'])) { print('$key holds $todo');}Every match must hold data of the type you pass, because Fuery casts the data to it. Keep detail keys like ['todo', 1] under another prefix than the list at ['todos'].
For anything else, read the caches. client.queryCache and client.mutationCache have getAll, find, and findAll:
final staleOnScreen = client.queryCache.findAll( const QueryFilters(type: QueryTypeFilter.active, stale: true),);final saving = client.mutationCache.findAll( const MutationFilters(status: MutationStatus.pending),);A match in the query cache is a cache entry (CachedQuery). A match in the mutation cache is the run of one mutate call (CachedMutation). Caches lists their fields. The caches are read-only: change the cache through the client.
Invalidating
Section titled “Invalidating”After a change on the server, mark the cache entries it affects stale:
client.invalidateQueries(queryKey: ['todos']); // ['todos'] and everything under itclient.invalidateQueries(queryKey: ['todos'], exact: true); // only ['todos']Fuery refetches the matching cache entries in use right away: those with an enabled observer, such as a mounted widget. The others refetch the next time something uses them. Pass refetchType: RefetchType.none to only mark them stale.
Choosing which queries an operation touches
Section titled “Choosing which queries an operation touches”invalidateQueries, refetchQueries, resetQueries, cancelQueries, removeQueries, and isFetching select cache entries with the same filters: queryKey, exact, type, stale, and predicate. See Operations on matching queries, Query filters, and Refetch and cancel arguments.
Refreshing only what is on screen
Section titled “Refreshing only what is on screen”client.invalidateQueries( queryKey: ['todos'], type: QueryTypeFilter.active,);Without type, Fuery marks every match stale and refetches the ones in use. With type: QueryTypeFilter.active, it touches only the ones in use. The others keep their data and stay fresh until their staleTime passes.
Clearing one user’s keys
Section titled “Clearing one user’s keys”When a prefix isn’t enough, a predicate reads the whole key:
client.removeQueries( predicate: (query) => query.queryKey.contains(userId),);The predicate receives the cache entry (CachedQuery), so it can also test query.state and query.options, for example to remove every cache entry whose fetch failed.
Watching the cache
Section titled “Watching the cache”client.watch turns any value computed from the client into a Stream, for example to show a loading bar while anything fetches:
client.watch((client) => client.isFetching() > 0); // any fetch runningclient.watch((client) => client.isMutating(mutationKey: ['todos']) > 0); // savingclient.watch((client) => client.getQueryData<List<Todo>>(['todos'])); // cached data- Each listener gets the current value first, then every new value after the query cache or the mutation cache changes.
- Fuery compares values as selectors do, so an equal value emits nothing.
- Watching fetches nothing.
Create the stream once, for example in a State field, and show it with a StreamBuilder:
class LoadingBar extends StatefulWidget { const LoadingBar({super.key});
@override State<LoadingBar> createState() => _LoadingBarState();}
class _LoadingBarState extends State<LoadingBar> { late final fetching = context.queryClient.watch( (client) => client.isFetching() > 0, );
@override Widget build(BuildContext context) { return StreamBuilder( stream: fetching, builder: (context, snapshot) => snapshot.data == true ? const LinearProgressIndicator() : const SizedBox.shrink(), ); }}In a bloc, listen to the stream like any other.
To show whether a mutation is running, widgets need no stream: MutationStateSelector shows it, and so does useMutationState in a HookWidget.
Fetching outside widgets
Section titled “Fetching outside widgets”client.query returns the cached data while it is fresh and fetches otherwise. Route guards, startup code, and prefetching use it:
final todos = await client.query(todosQuery); // fetch, or use fresh cacheclient.query(todosQuery).ignore(); // prefetch: ignore the result and errors- Data is fresh for the query’s
staleTime. With the default of zero,client.queryfetches every time. - A call that needs to fetch waits for a fetch already running for the key, instead of starting another.
- It throws when the fetch fails.
- It retries only when the query,
defaultOptions, or asetQueryDefaultscall for its key setsretry.
To use whatever is cached, however old, set staleTime: staticStaleTime. client.query then fetches only when nothing is cached.
Fetching an infinite query outside widgets
Section titled “Fetching an infinite query outside widgets”client.infiniteQuery does the same for infinite queries:
InfiniteQuery<TodoPage, int> pagedTodosQuery() => InfiniteQuery( queryKey: ['todos', 'paged'], queryFn: (context) => api.getPage(context.pageParam), initialPageParam: 1, getNextPageParam: (data) => data.lastPage.hasMore ? data.lastPageParam + 1 : null, pages: 3, );
await client.infiniteQuery(pagedTodosQuery());pagessets how many pages a fetch loads when nothing is cached: one by default, and never more thanmaxPages.pagesbelongs to the definition, so a widget that shows this query also loads three pages first.- When pages are cached, a fetch reloads them from the first, up to
maxPages, and ignorespages.
Clearing everything at logout
Section titled “Clearing everything at logout”Future<void> logout() async { await api.logout(); Fuery.client.clear();}clear() removes every cache entry and every mutation run, and deletes all persisted data. The client stays, with the defaults registered through setQueryDefaults and setMutationDefaults.
What happens to a running mutation depends on its stage:
- A mutation already sending its request finishes.
- A mutation still waiting, for the network or for its turn in a scope, fails with a
CancelledError.mutateAsyncthrows it, and the mutation’s state shows it. None of its callbacks run, including those ofMutationCacheConfigand of itsmutatecall, so no rollback writes the old session’s data back.
Clear once the screens that use queries are gone. An observer still subscribed when clear() or removeQueries runs moves to a new cache entry for the same key, which loads like a new one. A list still on screen therefore refetches right away, with the logged-out session. Navigate to the login screen first, and unsubscribe any observer you subscribed by hand.
In the example app
Section titled “In the example app”The example prefetches a post when the pointer hovers its card in the feed, and watches the client for an activity indicator in the home shell. Its README maps each screen to what it shows.