CancellationToken class
A one-shot cooperative cancellation signal.
Cancellation is idempotent and irreversible: the first cancel latches the state and records the reason; later calls are no-ops. Consumers poll throwIfCancelled, await whenCancelled, or register an onCancel callback. The token never preempts running code — see the library doc.
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isCancelled → bool
-
Whether cancel has been called.
Audited: 2026-06-12 11:26 EDT
no setter
- reason → Object?
-
The reason passed to cancel, or
nullif none was supplied / not yet cancelled. Audited: 2026-06-12 11:26 EDTno setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
whenCancelled
→ Future<
void> -
Completes once the token is cancelled; never completes with an error.
Audited: 2026-06-12 11:26 EDT
no setter
Methods
-
cancel(
[Object? reason]) → void -
Cancels the token with an optional
reason; a second call is a no-op. Audited: 2026-06-12 11:26 EDT -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onCancel(
void callback()) → void -
Registers
callbackto fire exactly once when the token is cancelled. -
throwIfCancelled(
) → void - Throws a CancellationException (carrying reason) if already cancelled. Audited: 2026-06-12 11:26 EDT
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited