cancel_token
Cancel Dart Futures and share a CancelToken across requests.
When to use
- Abort in-flight work when a host (screen, session, scope) is disposed
- Share one cancel signal across HTTP and tracked futures
- Nest per-request tokens under a parent that cancels children together
API
| Type | Role |
|---|---|
CancelToken |
Cancel signal; whenCancel, track, child link / linkWhile |
CancelableScope |
Owns tokens for a lifetime; run / dispose / cancel |
CancelledException |
Thrown when a scoped run is cancelled before completion |
Cancelable |
Interface implemented by tokens and scopes |
Usage
final scope = CancelableScope();
final value = await scope.run((token) async {
// Pass token into your HTTP / fetch layer (await token.whenCancel, etc.).
return fetchSomething(cancelToken: token);
});
// Later — abort everything still registered with the scope:
scope.dispose();
Runnable console demo:
cd example && dart pub get && dart run
See example/bin/cancel_token_example.dart.
Install
dependencies:
cancel_token: ^0.1.1
Coverage
Changelog
See CHANGELOG.md.
Maintainers
License
MIT. See LICENSE.
Copyright (c) 2026 Emil Zulufov emilzulufov566@gmail.com
Libraries
- cancel_token
- Cooperative cancellation for Dart Futures and HTTP-style cancel signals.