QueryClient class

Central manager for query caching and invalidation.

The QueryClient provides:

  • Centralized cache storage
  • Query invalidation
  • Prefetching
  • Cache manipulation
// Get the global client
final client = queryClient;

// Invalidate queries (triggers refetch)
client.invalidate(['users']);

// Invalidate with prefix matching
client.invalidate(['users'], exact: false);  // Matches ['users'], ['users', 1], etc.

// Prefetch for instant navigation
await client.prefetch(
  key: ['user', userId],
  queryFn: () => api.getUser(userId),
);

// Direct cache manipulation
client.setQueryData(['user', userId], updatedUser);

Constructors

QueryClient({Duration defaultStaleTime = Duration.zero, Duration defaultGcTime = const Duration(minutes: 5), int defaultRetry = 3, Duration defaultRetryDelay = const Duration(seconds: 1)})

Properties

defaultGcTime Duration
How long unused queries remain cached (default: 5 minutes).
final
defaultRetry int
Default number of retries on failure.
final
defaultRetryDelay Duration
Base delay between retries (uses exponential backoff).
final
defaultStaleTime Duration
How long data is considered fresh (default: always stale).
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

clear() → void
Clear the entire cache.
getQueryData<T>(QueryKey key) → T?
Get cached data for a query key.
invalidate(QueryKey key, {bool exact = false}) → void
Invalidate queries, causing them to refetch.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
prefetch<T>({required QueryKey key, required Future<T> queryFn(), Duration? staleTime}) Future<void>
Prefetch data and store in cache for later use.
remove(QueryKey key, {bool exact = false}) → void
Remove queries from the cache entirely.
setQueryData<T>(QueryKey key, T data) → void
Set data directly in the cache.
toString() String
A string representation of this object.
inherited

Operators

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