QueryOptions<TQueryData> class base Options

Everything the cache entry for a query needs: its queryKey, the queryFn that fetches it, and how long and how hard to keep it — staleTime, gcTime, retry, retryDelay, networkMode, enabled, initialData, structuralSharing and meta.

This is what the imperative QueryClient.query takes, which fetches once and completes with the data. An observer — a widget watching the query — takes QueryObserverOptions or QuerySelectOptions instead, which add what only a watcher cares about: when to refetch (refetchOnMount, refetchOnWindowFocus, refetchOnReconnect, refetchInterval), placeholderData, and select. Both extend this class, so one options helper can serve both paths.

null means unset on every option field. An unset field takes the default registered for the key with QueryClient.setQueryDefaults, then the client's DefaultOptions, then the library default each field's documentation names. An option with a real "off" value says so with a value — RefetchInterval.off, RetryPolicy.never, Enabled.no — never with null.

QueryObserverOptions<List<Todo>> todosQuery() => QueryObserverOptions(
      queryKey: QueryKey(['todos']),
      queryFn: (context) => api.fetchTodos(),
      staleTime: const StaleTime.duration(Duration(minutes: 1)),
    );

final todos = await client.query(todosQuery());

No value equality, on purpose: options built inline in a build are re-applied on every build, and the observer works out what actually changed by comparing the resolved values — so two inline closures for queryFn do not count as a change, and neither does a fresh Enabled.when(…).

Implementers
Annotations
  • @immutable

Constructors

QueryOptions({required QueryKey queryKey, QueryFn<TQueryData>? queryFn, Enabled? enabled, StaleTime? staleTime, GcTime? gcTime, RetryPolicy? retry, RetryDelay? retryDelay, NetworkMode? networkMode, InitialData<TQueryData>? initialData, DateTime? initialDataUpdatedAt, DateTime? initialDataUpdatedAtCompute()?, StructuralSharing<TQueryData>? structuralSharing, Object? meta, @internal FetchBehavior<TQueryData>? behavior})
Every field but queryKey is optional; an unset field takes the client's default when the query is built.
const

Properties

enabled → Enabled?
Whether the query may fetch on its own. The default: Enabled.yes. A disabled query still serves whatever the cache holds and can still be refetched by hand.
final
gcTime → GcTime?
How long the query stays cached after its last observer leaves. The default: GcTime.defaultValue, five minutes.
final
hashCode → int
The hash code for this object.
no setterinherited
initialData → InitialData<TQueryData>?
Data the cache starts with, as if it had been fetched — see InitialData. No default: unset, the query starts without data. Unlike placeholderData it is written to the cache, and it goes stale by staleTime from initialDataUpdatedAt on.
final
initialDataUpdatedAt → DateTime?
When initialData was fetched, for the staleness clock. No default: unset, the seed counts as fetched the moment it is written.
final
initialDataUpdatedAtCompute → DateTime? Function()?
Computes the seed timestamp only when data is actually seeded. No default. Mutually exclusive with initialDataUpdatedAt; a null result means now.
final
meta → Object?
Arbitrary data carried along for logging, devtools or a query function, which receives it as QueryFunctionContext.meta. No default: unset, the key's registered default applies, if any.
final
networkMode → NetworkMode?
How connectivity gates the fetch. The default: NetworkMode.online, an offline device pauses the fetch until it is back.
final
queryFn → QueryFn<TQueryData>?
Fetches the data. Unset, the query uses the function registered for its key with QueryClient.setQueryDefaults; with none there either, a fetch fails with MissingQueryFunctionError.
final
queryKey → QueryKey
The key this query is cached under. Required. Bound to exactly one data type: a key read as another type — a supertype included — throws QueryDataTypeError.
final
retry → RetryPolicy?
Whether a failed fetch is retried. The default: RetryPolicy.times(3), three retries after the first failure (TanStack Query: retry: 3). QueryClient.query is the exception: when neither these options nor the client's defaults set retry, its fetch is not retried.
final
retryDelay → RetryDelay?
How long to wait between attempts. The default: RetryDelay.defaultValue, exponential back-off from one second — one, two, four, … — capped at thirty.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
staleTime → StaleTime?
How long fetched data counts as fresh. The default: StaleTime.zero, stale the moment it arrives, so every mount, focus and reconnect refetches.
final
structuralSharing → StructuralSharing<TQueryData>?
How new data is reconciled with what the cache already holds — see StructuralSharing. The default: replaceEqualDeep, which keeps every deep-equal part of the previous data. noStructuralSharing turns it off.
final
toStringFields → Map<String, Object?>
What toString shows after the key: every field, in declaration order, with the unset (null) ones skipped — so a test failure or a debug print reads QueryOptions<int>(QueryKey(["a"]), staleTime: …) rather than Instance of 'QueryOptions<int>'. A subclass adds its own fields after these.
no setter

Methods

copyWith({QueryKey? queryKey, QueryFn<TQueryData>? queryFn, Enabled? enabled, StaleTime? staleTime, GcTime? gcTime, RetryPolicy? retry, RetryDelay? retryDelay, NetworkMode? networkMode, InitialData<TQueryData>? initialData, DateTime? initialDataUpdatedAt, DateTime? initialDataUpdatedAtCompute()?, StructuralSharing<TQueryData>? structuralSharing, Object? meta}) → QueryOptions<TQueryData>
This, with the given fields replaced. Passing null leaves a field as it is. Supplying one seed timestamp form clears its previous alternative.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
override

Operators

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