QueryCancelToken class Queries
Handed to a query function, as QueryFunctionContext.signal, so it can abort work that is no longer wanted.
Dart has no ecosystem-wide cancellation primitive, so onCancel is the universal interop point — forward it to whatever your HTTP client uses:
queryFn: (context) {
final cancelToken = dio.CancelToken();
context.signal.onCancel(cancelToken.cancel);
return dio.get('/tasks', cancelToken: cancelToken);
}
For a client that takes a future instead, pass whenCancelled.
package:http 1.5 and later aborts an AbortableRequest when its
abortTrigger completes, and the request then fails with
RequestAbortedException:
queryFn: (context) async {
final request = http.AbortableRequest(
'GET',
Uri.parse('https://api.example.com/tasks'),
abortTrigger: context.signal.whenCancelled,
);
final response = await http.Response.fromStream(
await client.send(request),
);
return parseTasks(response.body);
}
A client with no cancellation at all simply never registers a callback; the request then runs to completion and its result is discarded, as in TanStack Query. For long loops, poll isCancelled or call throwIfCancelled between steps, or await whenCancelled.
Reading signal matters: a query whose function never read it is not
cancelled when its last observer leaves — the request cannot be stopped,
so it is allowed to finish and its result is cached. Once the signal has
been read, the fetch is cancelled and the query reverts instead.
The library creates one token per fetch, shared by its retries, and
cancels it; a query
function never calls cancel. Tests that call a query function directly
create their own with the default constructor, QueryCancelToken(), and
pass it to the QueryFunctionContext they build.
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isCancelled → bool
-
Whether cancel has been called. Once
trueit staystrue: a token is never reused across fetches.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
whenCancelled
→ Future<
void> -
Completes when the fetch is cancelled, and never otherwise.
no setter
Methods
-
cancel(
) → void - Cancels this token. Called by the library, not by query functions.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onCancel(
void callback()) → void -
Runs
callbackon cancellation — immediately, if already cancelled. -
throwIfCancelled(
) → void - Throws a CancelledError if this token has been cancelled.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited