QueryClientProvider class Setup

Provides a QueryClient to the widgets below it, and wires the client to Flutter while it is mounted.

Put one above everything that reads a query — usually around the app:

void main() {
  runApp(
    QueryClientProvider.create(
      create: QueryClient.new,
      child: const MyApp(),
    ),
  );
}

QueryClientProvider.create makes the client and owns it: it lives as long as the provider and is cleared after the provider unmounts. The unnamed constructor takes a client you made yourself — one you configure with defaultOptions, share with code outside the tree, or create in a test — and never clears it:

final client = QueryClient(
  defaultOptions: DefaultOptions(
    queries: QueryDefaults(
      staleTime: const StaleTime.duration(Duration(seconds: 30)),
    ),
  ),
);

QueryClientProvider(client: client, child: const MyApp());

Below it, every call style finds the client on its own. Code that needs the client itself reads it with of (subscribes, for build), maybeOf (the same, null without a provider) or read (no subscription, for callbacks and initState):

onPressed: () => QueryClientProvider.read(context)
    .invalidateQueries(filters: QueryFilters(queryKey: tasksKey)),

Three things happen at mount:

  1. App lifecycle → focus. Every AppLifecycleState the app reports — the one it is already in, and each transition after — is mapped onto the client's focus state by isAppShown. resumed is focused; hidden, paused and detached are not. inactive depends on the platform, because the state means two different things: on iOS and Android it is a transient interruption (the notification shade, the app switcher, an incoming call) and counts as focused, since treating those as "unfocused" would refetch the world on the way back; on macOS, Windows and Linux it is precisely the window losing focus — the event refetchOnWindowFocus is named after — and counts as unfocused. Pass isAppShown to decide it yourself, or observeAppLifecycle false to switch the mapping off.
  2. The notify scheduler. Notifications that arrive while a build is in flight are deferred to a post-frame callback, so a query resolving mid-build cannot call setState during that build. Several providers may share one client — siblings, or an old and a new one for a frame — and the scheduler stays installed until the last of them goes.
  3. Connectivity, only if you bring it. Pass onlineStatus — an OnlineStatus.fixed value, or an OnlineStatus.stream with the assumption to start from — and the client follows it. Nothing is installed by default and no connectivity package is a dependency; see OnlineStatus for a connectivity_plus-style example.
Inheritance

Constructors

QueryClientProvider({Key? key, required QueryClient client, required Widget child, OnlineStatus? onlineStatus, bool observeAppLifecycle = true, bool isAppShown(AppLifecycleState state)?})
Provides client to child and wires it to Flutter for as long as this widget is mounted — the three things the class doc lists. A different client on a later build unmounts the old one and mounts the new.
const

Properties

child → Widget
The subtree that can reach client — through of, maybeOf and read, and through the builders, mixin and context.query built on them.
final
client → QueryClient
The client every builder, controller and context.query below runs on unless it names one of its own. Mounted while this widget is; swapping it recreates every observer below, because each belonged to the old client.
final
hashCode → int
The hash code for this object.
no setterinherited
isAppShown → bool Function(AppLifecycleState state)?
Which AppLifecycleStates count as "the user is looking at the app", and so as focused for refetchOnWindowFocus.
final
key → Key?
Controls how one widget replaces another widget in the tree.
finalinherited
observeAppLifecycle → bool
Whether to map the app's lifecycle onto the client's focus state. Defaults to true.
final
onlineStatus → OnlineStatus?
Optional connectivity, as one value: OnlineStatus.fixed for a client with no source of its own, OnlineStatus.stream for one that follows a stream from a stated starting assumption. true means "assume the network is reachable".
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

createElement() → StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() → State<QueryClientProvider>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() → List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) → String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String
Returns a one-line detailed description of the object.
inherited
toStringShort() → String
A short, textual description of this widget.
inherited

Operators

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

Static Methods

create({Key? key, required QueryClient create(), required Widget child, OnlineStatus? onlineStatus, bool observeAppLifecycle = true, bool isAppShown(AppLifecycleState state)?}) → Widget
Creates and owns a client for this widget's lifetime. Rebuilding with a different create callback keeps the client; change key to replace it. The owned client is cleared after its provider unmounts.
maybeOf(BuildContext context) → QueryClient?
The nearest client above context, or null when there is none — for a widget that can do without one. Subscribes like of.
of(BuildContext context) → QueryClient
The nearest client above context.
read(BuildContext context) → QueryClient
Like of, but without subscribing the calling element to changes — the one to use in callbacks, initState and anywhere else outside build. Throws when there is no provider.