Skip to content
pubscout

cancellation_token

2.0.1, released 3 years ago by jonathancole.dev

Easy async task cancellation for tasks using cancellation tokens in Dart.

WASM-ready
$ flutter pub add cancellation_token
Source repository

What it does

Provides CancellationToken utilities to cancel futures, isolates, completers, and custom async operations. It allows cleaning up resources and propagating cancellation exceptions across tasks.

AI-written summary of version 2.0.1, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to cancel pending Futures or Isolates when a widget is disposed.
  • You want to share a single CancellationToken across multiple async tasks.

Skip it when

  • You only need HTTP request cancellation and can use a dedicated HTTP client package instead.

Strengths

  • Supports futures, completers, isolates, and custom cancellables.
  • Helps prevent memory leaks and unneeded processing when widgets dispose.

Weaknesses

  • Requires manual plumbing or specific wrapper classes for existing async functions.

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
2
CancellationToken cancellationToken = CancellationToken();

Future<void> loadData() async {
  try {
    someDataA = await getDataA().asCancellable(cancellationToken);
  } on CancelledException {
    // Ignore cancellations
  }
}

Alternatives

Packages that do the same job and could replace cancellation_token.

  • secretary

    A Dart library for managing queues of asynchronous tasks with validation, retries and event streams.

  • async_queue

    Ensure async tasks execute sequentially in order, one after the other.

  • executor

    Executes async tasks with a configurable maximum concurrency and rate.

Works with

Packages commonly used together with cancellation_token: ones it builds on, ones built on it, and its own family.