QueryCache class Caches

Every query of one QueryClient, stored under its QueryKey.

A client owns exactly one query cache, and usually creates it itself: QueryClient() makes an empty one. Queries are added as observers and fetches first use a key, and leave again when they are garbage-collected (after gcTime without observers) or removed. Most code never touches the cache directly — the client's methods (getQueryData, invalidateQueries, ...) work through it.

Reasons to reach for it:

  • Cache-wide hooks. Pass onSuccess, onError or onSettled to the constructor, and the cache to the client, to handle every query's outcome in one place — the usual home of a global error report.
  • Events. subscribe delivers a QueryCacheEvent for every query that is added, removed or updated — for logging, devtools or a persistence layer.
  • Lookup. find, findAll and queries return the Query entries themselves, with their full state.
final client = QueryClient(
  queryCache: QueryCache(
    onError: (error, stackTrace, query) {
      // Only background refreshes: a first load shows its own error.
      if (query.state.hasData) {
        showSnackBar('Could not refresh ${query.queryKey}: $error');
      }
    },
  ),
);

Constructors

QueryCache({void onSuccess(Object? data, Query<Object?> query)?, void onError(Object error, StackTrace stackTrace, Query<Object?> query)?, void onSettled(Object? data, Object? error, StackTrace? stackTrace, Query<Object?> query)?})
Creates an empty cache with the given cache-wide hooks, all optional. A QueryClient constructs one of these when none is passed to it.

Properties

hashCode → int
The hash code for this object.
no setterinherited
hasListeners → bool
Whether at least one listener is registered — what tells a manager to keep its platform event source installed.
no setterinherited
listeners → List<void Function(QueryCacheEvent event)>
The listeners currently registered, in subscription order, read-only. Subclasses notifying by hand iterate this; notifyListeners is the loop that skips a listener an earlier one removed.
no setterinherited
onError → void Function(Object error, StackTrace stackTrace, Query<Object?> query)?
Runs after any query's fetch fails for good — once retries are exhausted, not per attempt — with the error, its stack trace and the query. Unset by default.
final
onSettled → void Function(Object? data, Object? error, StackTrace? stackTrace, Query<Object?> query)?
Runs after any query's fetch settles, success or failure, with whichever of data and error applies: after a failure, data is what the query still holds from an earlier success, or null. Runs right after onSuccess or onError, once per fetch, and is skipped when that hook throws; a fetch whose cancel records no error runs neither. Unset by default.
final
onSuccess → void Function(Object? data, Query<Object?> query)?
Runs after any query's fetch succeeds, with the fetched data and the query. Unset by default.
final
queries → List<Query<Object?>>
Every query in the cache, as a copy: safe to iterate while removing. TanStack Query calls this getAll.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

add(Query<Object?> query) → void
Puts query into the cache under its key and emits QueryAdded. A key already present keeps the query it has; nothing is replaced. build is the usual way in — this is for a query constructed by hand.
build<TQueryData>(QueryClient client, DefaultedQueryOptions<TQueryData> options, {QueryState<TQueryData>? state}) → Query<TQueryData>
The query for options's key, creating it if it does not exist yet.
clear() → void
Removes every query, one QueryRemoved each.
find({required QueryFilters filters}) → Query<Object?>?
The first query matching filters, or null. Unlike findAll, an unset exact means an exact key match here, so find(filters: QueryFilters(queryKey: key)) finds the query stored under key itself.
findAll({QueryFilters filters = const QueryFilters()}) → List<Query<Object?>>
Every query matching filters, in insertion order — all of them when the filters are empty. A queryKey filter matches as a prefix unless exact is true.
get<TQueryData>(QueryKey queryKey) → Query<TQueryData>?
The query stored under queryKey, or null if there is none.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notify(QueryCacheEvent event) → void
Delivers event to every listener added with subscribe.
notifyListeners(void deliver(void listener(QueryCacheEvent event))) → void
Calls every listener through deliver. See ListenerRegistry.notify: a listener an earlier one removed is skipped, and a throw is reported to the zone rather than escaping into whatever produced the event.
inherited
onFocus({bool refetchQueries = true}) → void
Every query reacts to the app coming back to the foreground. Setting refetchQueries to false still allows paused requests to continue.
onOnline() → void
Every query reacts to the device coming back online.
onSubscribe() → void
Called after every subscribe. Subclasses use it to install their event source when the first listener arrives.
inherited
onUnsubscribe() → void
Called after every unsubscribe. Subclasses use it to tear their event source down once hasListeners turns false.
inherited
remove(Query<Object?> query) → void
Takes query out of the cache, cancelling its fetch silently and stopping its collection timer, then emits QueryRemoved. The key's slot is only cleared when it still holds this very query — a stale reference cannot evict its successor. QueryClient.removeQueries does the same for every query matching a filter.
subscribe(void listener(QueryCacheEvent event)) → void Function()
Registers listener and returns the function that removes it again.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited