Query<TQueryData> class
Queries
One cache entry: a key, its options, its state, and the fetch machinery.
Application code does not construct a Query; the QueryCache does,
the first time an observer or a fetch uses a key. You meet one when you
look at the cache from outside:
QueryCache.findandQueryCache.findAll, orQueryCache.queries;- the
queryof everyQueryCacheEventa cache listener receives, and thequerypassed to the cache'sonSuccess/onErrorhooks; - the argument of a
QueryFilters.predicate, and of callbacks such asStaleTime.dynamicandRefetchInterval.dynamic.
What it offers:
- Reading: queryKey, state (data, error, statuses, counters), options, meta, observersCount, isStale, isActive.
- Acting: fetch, invalidate, cancel, reset, setState. The
client's filtered methods (
invalidateQueries,cancelQueries,resetQueries, ...) call these for every matching query and are the usual way in.
A query is removed from the cache after gcTime with no observers, or
through QueryClient.removeQueries; a removed instance never comes back,
and the next use of the key creates a fresh one.
It has one type parameter, the data type the cache stores — errors are
an Object with a StackTrace, and select belongs to the observer,
not the query.
Constructors
-
Query({required QueryClient client, required QueryCacheRef cache, required QueryKey queryKey, required DefaultedQueryOptions<
TQueryData> options, QueryState<TQueryData> ? state}) -
Creates a cache entry for
queryKeywithoptions, seeded frominitialDataor from a restoredstate, and arms its collection timer straight away — a query nobody observes yet is collectable. Constructed byQueryCache.build; user code reaches a query through the cache.
Properties
- client → QueryClient
-
The client this query belongs to: the source of its managers and its
notify batching.
final
- dataType → Type
-
The data type this query holds, exactly.
no setter
-
future
→ Future<
TQueryData> ? -
The in-flight fetch, if there is one: the future every caller of fetch
shares, settled once the cache has been written. Read from another error
zone than the fetch began in, it is that zone's own (
zones.dart).no setter - gcTime → GcTime?
-
How long this may sit unused before it is collected.
nulluntil options are applied. Readable, as in TanStack Query — devtools and tests read the value that actually won — but onlyupdateGcTimemoves it, because the longest request has to keep winning.no setterinherited - hashCode → int
-
The hash code for this object.
no setterinherited
- meta → Object?
-
The options'
meta: free-form data attached to the query, for cache listeners, cache hooks and devtools to read.no setter -
observers
→ List<
QueryObserverRef> -
The observers attached right now, read-only;
addObserverandremoveObserverare the only way in and out.no setter - observersCount → int
-
How many observers are attached right now — zero for a query nothing
on screen uses, which is what makes it collectable after
gcTime.no setter -
options
→ DefaultedQueryOptions<
TQueryData> -
The options in force, fully resolved. They are the last options any
observer or fetch handed in;
queryFn,retry,networkModeand the like are read from here at fetch time. An imperative client fetch that omits retry preserves the entry's retry policy and uses its no-retry override only for that fetch.no setter - queryKey → QueryKey
-
The key this query is stored under in the cache.
final
-
resetState
→ QueryState<
TQueryData> -
The state this query returns to on reset: pending with no data, or
the
initialDataseed its options provided.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
state
→ QueryState<
TQueryData> -
The current state: data, error, status, fetch status and the counters.
Replaced on every dispatch; observers compute their results from it.
no setter
Methods
-
cancel(
{bool revert = false, bool silent = false}) → Future< void> - Cancels the in-flight fetch, completing once it has settled. Does nothing when no fetch is running.
-
clearGcTimeout(
) → void -
Cancels the collection timer without touching
gcTime. Called whenever an observer attaches or a fetch starts, so a used entry is never collected.inherited -
fetch(
{DefaultedQueryOptions< TQueryData> ? options, FetchOptions? fetchOptions}) → Future<TQueryData> - Fetches this query now: runs its query function, with the retries and network rules of its options, and completes with the data.
-
invalidate(
) → void -
Marks the data stale regardless of
staleTime, dispatching QueryInvalidateAction unless it already is. Nothing is refetched here;QueryClient.invalidateQueriescalls this and then refetches the queries itsrefetchTypenames. -
isActive(
) → bool -
Whether any observer's
enabledresolves to true — whether something is actively using this query. This is whatQueryTypeFilter.activeandRefetchType.activeselect. -
isDisabled(
) → bool - Whether the query will not fetch on its own.
-
isFetched(
) → bool - Whether at least one fetch — or a manual write — has ever settled.
-
isStale(
) → bool - Whether the data should be refetched: any observer's result says so, or — with no observers — there is no data or it has been invalidated.
-
isStaleByTime(
StaleTime staleTime) → bool -
Whether the data is older than
staleTime. -
isStatic(
) → bool -
Whether any attached observer uses
StaleTime.static, declaring this query permanently fresh: not even an invalidation makes it stale. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
optionalRemove(
) → void - Removes this from its cache if nothing is using it.
-
reset(
) → void -
Back to the state this query was created with (resetState),
cancelling any fetch in flight. Observers are notified of the reset
state; nothing is refetched here.
QueryClient.resetQueriescalls this for every matching query and then refetches the active ones. -
scheduleGc(
) → void - Arms the collection timer for the current gcTime, replacing any that is already running. Called when the last observer leaves; a GcTime.never arms nothing.
-
setState(
QueryState< TQueryData> state) → void - Replaces this query's state wholesale and notifies its observers and the cache's listeners, with a QuerySetStateAction.
-
toString(
) → String -
A string representation of this object.
override
-
updateGcTime(
GcTime? newGcTime) → void -
Folds
newGcTime(or the five-minute default whennull) intogcTime, keeping whichever of the old and new values is longer.inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited