InfiniteQueryObserver<TPageData, TPageParam, TData> class
Observers
Watches one infinite query, and pages it.
Everything QueryObserver does applies — subscribe, currentResult,
refetch, setOptions, destroy — with an InfiniteData as the
query's data (or whatever a select makes of it). On top, it adds:
- paging: fetchNextPage and fetchPreviousPage;
- whether there is more: hasNextPage and hasPreviousPage;
- which fetch is running or failed: isFetchingNextPage, isFetchingPreviousPage, isFetchNextPageError, isFetchPreviousPageError, and isRefetching / isRefetchError narrowed to refetches of the pages already held.
These live on the observer rather than on the result, because the sealed QueryResult is shared by every kind of query. A listener is still told when one of them changes, whether or not the result did, so reading them in the listener is always up to date. (TanStack Query puts them on its infinite result object instead.)
final observer = InfiniteQueryObserver(
client,
InfiniteQueryObserverOptions<List<Post>, int>(
queryKey: QueryKey(['feed']),
pageFn: (context) => api.feed(offset: context.pageParam),
initialPageParam: 0,
getNextPageParam: (page, pages, pageParam, pageParams) =>
page.isEmpty ? null : pageParam + page.length,
),
);
final unsubscribe = observer.subscribe((result) {
if (result case QuerySuccess(:final data)) {
render(data.flatten<Post>(), canLoadMore: observer.hasNextPage);
}
});
// When the user scrolls to the end:
if (observer.hasNextPage && !observer.isFetchingNextPage) {
await observer.fetchNextPage();
}
- Inheritance
-
- Object
- QueryObserver<
InfiniteData< TPageData, TPageParam> , TData> - InfiniteQueryObserver
Constructors
-
InfiniteQueryObserver(QueryClient client, InfiniteQueryObserverOptionsBase<
TPageData, TPageParam, TData> options) -
Creates an observer for
options, whose paging half becomes the query's fetch behaviour; otherwise exactly QueryObserver.new.
Properties
- client → QueryClient
-
The client this observer reads through. Infinite observers need it to
re-default their options.
no setterinherited
-
currentQuery
→ Query<
InfiniteData< TPageData, TPageParam> > -
The cache entry this observer is watching right now. Changes when
setOptions is given a different key, or when the previous entry was
collected while nobody was listening.
no setterinherited
-
currentResult
→ QueryResult<
TData> -
The result as of now — the last one computed, which is also the last
one listeners were told about unless getOptimisticResult has replaced
it since.
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether anyone is subscribed (TanStack Query calls this "mounted").
Fetch-on-mount, the stale timer and the polling timer only run while
this is true.
no setterinherited
- hasNextPage → bool
-
Whether fetchNextPage would fetch anything: there are pages, and
getNextPageParamreturns a param for the last one. False before the first page has arrived.no setter - hasPreviousPage → bool
-
Whether fetchPreviousPage would fetch anything: there are pages,
getPreviousPageParamis set, and it returns a param for the first one. False before the first page has arrived.no setter -
infiniteOptions
→ InfiniteQueryOptions<
TPageData, TPageParam> -
The paging half of the current options.
no setter
- isFetchingNextPage → bool
-
Whether a fetchNextPage is in flight — the "loading more" state at
the end of a list. A refetch of the held pages does not count.
no setter
- isFetchingPreviousPage → bool
-
Whether a fetchPreviousPage is in flight — the "loading more" state
at the start of a list.
no setter
- isFetchNextPageError → bool
-
Whether the query's error came from a fetchNextPage rather than a
refetch or the initial load. The pages already held are still there;
a UI typically shows a retry button at the end of the list.
no setter
- isFetchPreviousPageError → bool
-
Whether the query's error came from a fetchPreviousPage rather than
a refetch or the initial load.
no setter
- isRefetchError → bool
-
The error twin of isRefetching: a refetch failed, not a page fetch.
no setter
- isRefetching → bool
-
Whether the pages already held are being refetched — not a page being
added. The result's own
isRefetchingis also true while a page is fetched, because the query is fetching and has data; this one leaves page fetches out.no setter -
options
→ DefaultedQueryObserverOptions<
InfiniteData< TPageData, TPageParam> , TData> -
The options in force, fully resolved against the client's defaults.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
createResult(
Query< InfiniteData< query, DefaultedQueryObserverOptions<TPageData, TPageParam> >InfiniteData< options, {bool optimistic = false}) → QueryResult<TPageData, TPageParam> , TData>TData> -
Turns
query's state into the result this observer reports underoptions: applies placeholder data while the query has none, runsselect(memoised on its input and the selector), turns a throwingselectinto a QueryError that keeps the last selected value as its stale data, and fills in the derived flags such asisStale.inherited -
destroy(
) → void -
Stops observing: clears listeners and timers and leaves the query, which
starts its
gcTimeclock.inherited -
executeFetch(
{bool cancelRefetch = false, Object? meta}) → Future< void> -
Runs the query's fetch with this observer's options; the error, if
any, lands in the query's state and is never rethrown.
inherited
-
fetchNextPage(
{bool cancelRefetch = true}) → Future< QueryResult< TData> > - Fetches the page after the ones already held and appends it.
-
fetchPreviousPage(
{bool cancelRefetch = true}) → Future< QueryResult< TData> > - Fetches the page before the ones already held and prepends it.
-
getOptimisticInfiniteResult(
InfiniteQueryObserverOptionsBase< TPageData, TPageParam, TData> options) → QueryResult<TData> - The result these options would produce right now — the infinite twin of QueryObserver.getOptimisticResult, for a binding's first build.
-
getOptimisticResult(
QueryObserverOptionsBase< InfiniteData< options) → QueryResult<TPageData, TPageParam> , TData>TData> -
See setOptions; getOptimisticInfiniteResult is the typed form.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
refetch(
{bool cancelRefetch = true}) → Future< QueryResult< TData> > -
Fetches the query again, whether or not its data is stale, and
completes with the currentResult after the fetch settled.
inherited
-
setInfiniteOptions(
InfiniteQueryObserverOptionsBase< TPageData, TPageParam, TData> options) → void - Replaces the options, taking the infinite options a user writes — the typed form of setOptions, which behaves the same otherwise: a new key moves the observer to another query, and a change that makes a fetch due starts one.
-
setOptions(
QueryObserverOptionsBase< InfiniteData< options) → voidTPageData, TPageParam> , TData> -
Accepts any options that carry the paging behaviour — what
setInfiniteOptions,
QueryClient.infiniteObserverOptionsand an InfiniteQueryOptions's ownbehaviorproduce. Plain observer options have no paging half; they would strip the behaviour from the shared query, whose next refetch would fail withMissingQueryFunctionError, so they are refused with an UnsupportedError.override -
shouldNotify(
QueryResult< TData> ? previous, QueryResult<TData> next) → bool -
The base rule, or a change in any paging flag since the last
notification. The flags are not part of the result, so without this a
fetchPreviousPagecancelling afetchNextPage(same data, still fetching, other direction) and asetOptionswhose newgetNextPageParamsays "no more" (same result,hasNextPagenow false) would leave listeners — and a "load more" button — unaware.override -
subscribe(
QueryObserverListener< TData> listener) → void Function() -
Registers
listenerand returns the function that removes it again.inherited -
toString(
) → String -
A string representation of this object.
inherited
-
updateResult(
) → void -
Recomputes the result from the query's current state and notifies
listeners if it changed — as shouldNotify decides.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited