CancellationToken class

Cancels one or more in-flight requests.

Create a token, pass it via RequestOptions, and cancel it when the work is no longer needed — typically from a dispose():

final _token = CancellationToken();

@override
void dispose() {
  _token.cancel('screen closed');
  super.dispose();
}

A cancelled request completes with ApiResponse.cancelled as its status. Note that cancellation stops the caller waiting on the response; it does not guarantee the underlying socket is torn down, since package:http exposes no per-request abort.

Constructors

CancellationToken()

Properties

cause → RequestCancelledException?
The exception this token was cancelled with, if any.
no setter
hashCode → int
The hash code for this object.
no setterinherited
isCancelled → bool
Whether cancel has been called.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
whenCancelled → Future<RequestCancelledException>
Completes when the token is cancelled. Never completes otherwise.
no setter

Methods

cancel([String? reason]) → void
Cancels every request holding this token. Calling it more than once has no further effect.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
throwIfCancelled() → void
Throws if this token has already been cancelled.
toString() → String
A string representation of this object.
inherited

Operators

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