CancellationToken class
A token for controlling the cancellation of Cancellable operations.
A single token can be used for multiple Cancellable operations.
Example implementation
CancellationToken cancellationToken = CancellationToken();
@override
void initState() {
super.initState();
loadData();
}
@override
void dispose() {
cancellationToken.cancel();
super.dispose();
}
Future<void> loadData() async {
loading = false;
try {
someDataA = await getDataA(cancellationToken: cancellationToken);
someDataB = await getDataB(cancellationToken: cancellationToken);
setState(() {});
} on CancelledException {
// Ignore cancellations
} catch (e, stackTrace) {
error = true;
}
}
See also:
- MergedCancellationToken, a CancellationToken that combines multiple tokens together.
- TimeoutCancellationToken, a CancellationToken that automatically cancels after a given duration.
- Implementers
Constructors
Properties
- exception → Exception
-
The exception given when the token was cancelled.
read-only
- hasCancellables → bool
-
Whether or not the token has any attached cancellables.
read-only
- hashCode → int
-
The hash code for this object.
read-onlyinherited
- isCancelled → bool
-
Whether or not the token has been cancelled.
read-only
- runtimeType → Type
-
A representation of the runtime type of the object.
read-onlyinherited
Methods
-
attach(
Cancellable cancellable) → void - Attaches a Cancellable to this token.
-
cancel(
[Exception exception = const CancelledException()]) → void - Cancels all operations with this token.
-
detach(
Cancellable cancellable) → void - Detaches a Cancellable from this token.
-
merge(
CancellationToken other) → MergedCancellationToken - Merges this CancellationToken with another to create a single token that will be cancelled when either token is cancelled.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited