MutationObserver<TData, TVariables, TOnMutateResult> class
Observers
Runs mutations and reports the latest one's state as a MutationResult.
This is the pure-Dart entry point for writes; the Flutter binding wraps one for its widgets. The lifecycle:
- Create it with the client and the MutationOptions. It starts idle; nothing runs yet.
- Subscribe to hear each new result; currentResult holds the latest one at any time.
- mutate (fire and forget) or mutateAsync (a future of the data) starts a run with the given variables, optionally with per-call MutateCallbacks. Each call builds a new mutation in the cache, and the observer follows the newest one.
- reset goes back to idle, cancel fails the run in flight, and setOptions swaps the options (a rebuild's new callbacks, say).
- Unsubscribe and destroy when done. A mutation already running
finishes on its own and is collected after its
gcTime.
final observer = MutationObserver<Todo, String, void>(
client,
MutationOptions(
mutationFn: (String title) => api.addTodo(title),
onSuccess: (todo, title, _) => client.invalidateQueries(
filters: QueryFilters(queryKey: QueryKey(['todos']))),
),
);
final unsubscribe = observer.subscribe((result) => print(result.status));
observer.mutate('Buy milk');
// Later:
unsubscribe();
observer.destroy();
- Implemented types
Constructors
-
MutationObserver(QueryClient _client, MutationOptions<
TData, TVariables, TOnMutateResult> options) -
Creates an idle observer for
options, resolved against the client's defaults. Nothing runs until mutate or mutateAsync is called.
Properties
-
currentResult
→ MutationResult<
TData, TVariables> -
The most recently computed result — idle until the first
mutate, then whatever the observed mutation's state maps to.no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether anyone is subscribed. Per-call callbacks only run while this is
true — a
mutatefrom a widget that has since gone must not call back into it.no setter -
options
→ DefaultedMutationOptions<
TData, TVariables, TOnMutateResult> -
The options in force, fully resolved against the client's defaults.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
cancel(
) → void -
Cancels the run this observer is showing — see Mutation.cancel: it
fails with a
CancelledError, its error callbacks run, and this observer shows that. Earlier runs this observer has moved on from are not touched, and with nothing running this does nothing. -
destroy(
) → void -
Stops observing for good: drops every listener and leaves the mutation,
which starts its
gcTimeclock. Call it when whatever owns the observer goes away; without it, a mutation that ran without listeners keeps an observer nobody will ever remove and can never be collected. (TanStack Query has no counterpart; a JavaScript observer is simply forgotten.) -
mutate(
TVariables variables, {MutateCallbacks< TData, TVariables, TOnMutateResult> ? callbacks}) → void -
Starts a run with
variablesand returns at once. Errors reach the callbacks and the result, never the zone — an unawaited failing future would otherwise surface as an uncaught error.callbacksare the per-call callbacks for this run only. -
mutateAsync(
TVariables variables, {MutateCallbacks< TData, TVariables, TOnMutateResult> ? callbacks}) → Future<TData> -
Starts a run with
variablesand completes with the data, or throws the error — after the callbacks have run, as mutate's result settles. The same order applies:onMutateat submission, the function in its scope's turn. Use it when the caller must await the write; otherwise prefer mutate. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reset(
) → void - Detaches from the mutation being observed and goes back to idle.
-
setOptions(
MutationOptions< TData, TVariables, TOnMutateResult> options) → void -
Replaces the options, resolving them against the client's defaults. A
changed
mutationKeymeans a different mutation, so the observer resets; otherwise a mutation still in flight takes the new options and a settled one keeps the ones it ran with. -
subscribe(
MutationObserverListener< TData, TVariables> listener) → void Function() -
Registers
listenerand returns the function that removes it again. The first listener re-attaches the observer to the mutation it was watching and brings the result up to date; the last one leaving detaches it, which starts the mutation'sgcTimeclock. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited