tanstack_query library
Flutter implementation of TanStack Query patterns: fetching, caching, invalidation, and background updates. Exports hooks and core types used by Flutter widgets.
Classes
- DefaultOptions
- Container for default query and mutation options.
-
FetchContext<
T> - A mutable context object passed to QueryBehavior.onFetch.
- FetchMeta
- Metadata passed to a fetch to indicate pagination behavior.
- FetchMore
- Metadata for fetching more pages.
- FocusManager
-
FocusManager mirrors the JS
focusManagerbehavior: it is subscribable and allows registering a platform-specific event listener which callssetFocusedto notify subscribers of focus changes. -
InfiniteQueryBehavior<
T> -
Flutter port of React's
infiniteQueryBehavior(). -
InfiniteQueryOptions<
T> - Options specific to infinite queries.
-
InfiniteQueryResult<
T> -
Result type returned by useInfiniteQuery, with helper
fetchNextPageandisFetchingNextPageflag. -
MutateOptions<
T> -
Per-mutation call options passed to
mutate. -
Mutation<
T, P> - A single mutation instance that holds its state and notifies observers on lifecycle events.
-
MutationAction<
T> - MutationCache
-
Mutation cache that stores mutation instances and allows subscribing to
mutation lifecycle events. It mirrors the JS
MutationCacheAPI. - MutationCacheConfig
-
Configuration callbacks for mutation lifecycle events such as
onMutate,onSuccess, andonError. - MutationCacheNotifyEvent
-
Notification event emitted by
MutationCache.subscribe. - MutationDefaultOptions
- Default options placeholder for mutations.
- MutationFunctionContext
-
Context object provided to mutation callbacks.
Contains the
clientand an optionalmutationKeywhich is the serialized cache-key produced byqueryKeyToCacheKeywhen amutationKeylist is supplied touseMutation. -
MutationObserver<
T, P> -
Observer that mirrors the JS
MutationObserver. It can be subscribed to by multiple listeners and will reflect the state of the underlying activeMutationinstance. -
MutationObserverResult<
T, P> - The shape returned by an observer to listeners.
-
MutationOptions<
T, P> - Options used to create a Mutation/Observer.
-
MutationResult<
T, P> -
MutationState<
T> - Represents the current state of a mutation operation.
- OnlineManager
-
OnlineManager mirrors the JS
onlineManagerbehavior: subscribable with custom event listener andsetOnline/isOnlinehelpers. -
Query<
T> -
Minimal
Queryimplementation to centralize fetch and observer logic. -
QueryBehavior<
T> - QueryCache
-
Query cache that stores cache entries and provides utilities to
find/subscribe/clear entries. It is intended to be owned by a
QueryClientinstance. - QueryCacheConfig
-
Configuration callbacks for query cache events like
onError,onSuccessandonSettled. -
QueryCacheEntry<
T> - A cache entry storing the last query result, a timestamp and optionally a running queryFnRunning future.
- QueryCacheNotifyEvent
-
A notification event emitted by
QueryCache. - QueryClient
-
Core client that owns the cache and provides utilities to invalidate,
set, and notify query data. Prefer obtaining the active client via
useQueryClient()(withQueryClientProvider) in hooks and widgets; a - QueryClientContext
-
A lightweight holder used to provide a
QueryClientinstance to the widget subtree viaProvider<QueryClientContext>. - QueryClientProvider
-
Provides a
QueryClientto its descendants. - QueryDefaultOptions
- Default options applied to queries (enabled, staleTime, refetch policy).
-
QueryObserver<
TQueryFnData, TError, TData> -
A simplified
QueryObserverthat mirrors the behavior of the JS implementation insofar as it maintains a current result based on a Query, can be subscribed to by multiple listeners, and can trigger refetch. -
QueryOptions<
T> -
QueryResult<
T> -
Represents the current state of a query, including
status, optionaldata,errorand whether a fetch is ongoing. -
Retryer<
T> -
TrackedFuture<
T> -
Simple wrapper to track whether a
Futurehas completed or failed. Used by the cache to detect running or errored fetches.
Enums
- FetchDirection
- Direction for infinite pagination fetches.
- MutationActionType
- Action emitted by a Mutation to observers.
- MutationStatus
- Status values for mutations.
- NotifyEventType
- Types of events emitted by the cache.
- QueryCacheEventType
- The kinds of events emitted by the query cache.
- QueryStatus
- Status values for queries.
Properties
- focusManager → FocusManager
-
final
- onlineManager → OnlineManager
-
final
Functions
-
customEncode(
dynamic item) → dynamic -
Converts enum values and other items into a string form suitable for
JSON encoding by
queryKeyToCacheKey. -
hasNextPage<
T> (InfiniteQueryOptions< T> options, List<T> ? data) → bool -
hasPreviousPage<
T> (InfiniteQueryOptions< T> options, List<T> ? data) → bool -
queryKeyToCacheKey(
List< Object> queryKey) → String -
Serializes a
queryKeyinto a deterministic string usable as a cache key. -
resetValues<
T> (ObjectRef< int> currentPage, int initialPageParam, ValueNotifier<InfiniteQueryResult< result, {bool isLoading = false}) → voidT> > - Resets the pagination and result state for an infinite query. Parameters:
-
useInfiniteQuery<
T> ({required List< Object> queryKey, required Future<T?> queryFn(int pageParam), bool? enabled, required int initialPageParam, int? getNextPageParam(T lastResult)?, double? staleTime, bool? refetchOnWindowFocus, bool? refetchOnReconnect, bool? refetchOnMount, int? gcTime, dynamic retry, bool? retryOnMount, dynamic retryDelay, Object? initialData, Object? initialDataUpdatedAt, Object? placeholderData, int? getPreviousPageParam(T firstResult)?}) → InfiniteQueryResult<T> - Hook for paginated/infinite queries.
-
useMutation<
T, P> ({required Future< T> mutationFn(P), List<Object> ? mutationKey, void onSuccess(T?, [MutationFunctionContext? context])?, void onError(Object?, [MutationFunctionContext? context])?, void onSettled(T?, Object?, [MutationFunctionContext? context])?, dynamic retry, dynamic retryDelay, int? gcTime}) → MutationResult<T, P> - Hook to perform mutations (writes) and track mutation state.
-
useQuery<
T> ({required List< Object> queryKey, required Future<T> queryFn(), int? gcTime, bool? enabled, Object? initialData, Object? initialDataUpdatedAt, Object? placeholderData, bool? refetchOnMount, bool? refetchOnReconnect, bool? refetchOnWindowFocus, dynamic retry, bool? retryOnMount, dynamic retryDelay, double? staleTime}) → QueryResult<T> -
Subscribe to a query identified by
queryKeyand manage its lifecycle. -
useQueryClient(
) → QueryClient -
Returns the nearest
QueryClientprovided byQueryClientProvider.
Typedefs
- FocusListener = void Function(bool focused)
-
InitialDataFn<
T> = T? Function() -
Typedef for a function that returns initial data (used by
initialData). - InitialDataUpdatedAtFn = int? Function()
-
Typedef for a function that returns the
initialDataUpdatedAttimestamp. - MutationCacheListener = void Function(MutationCacheNotifyEvent event)
- Listener signature for cache-level notifications.
-
MutationObserverListener<
T, P> = void Function(MutationObserverResult< T, P> ) - Listener type for mutation observers.
- OnlineListener = void Function(bool online)
-
PlaceholderDataFn<
T> = T? Function(T? previousValue, dynamic previousQuery) - Typedef for placeholder data function: takes previous value and previous query (observer-only) and returns a value.
- QueryCacheListener = void Function(QueryCacheNotifyEvent event)
- Listener signature for cache-level notifications.
-
QueryObserverListener<
T, E> = void Function(QueryResult< T> ) -
Listener typedef used by
QueryObserver.
Exceptions / Errors
- CancelledError
- Exception thrown when a query or mutation is cancelled.