Skip to content

subscription_never_cancelled

Reports a stream subscription that nothing keeps, in a class that has dispose or close. Keep it in a field and cancel it there.

Tier
blocking
Scope
Dart
Lowest preset
recommended
Fix
none

A listen of dart:async that is a statement of its own, so its subscription is thrown away, in a class whose objects end. Such a class declares or inherits an instance method named dispose or close: a ChangeNotifier, a State, a Cubit, or a class of your own. A listen in a method, a constructor or a closure of the class counts. The violation covers the call.

It does not report a stream that may end by itself or with the object, or one whose source the code keeps and may end:

  • a stream made from values or a future (Stream.value, Stream.fromIterable, Stream.fromFuture, Stream.empty, future.asStream()), or cut short with take;
  • a stream read from a field that the class releases, by calling close, dispose, cancel or a method with one of them as a word on it, or that it names in a method of its own whose name says release, stop, kill, terminate or shutdown. Examples are the stream of its own controller, of a cubit it closes, or of a process it kills.
  • a stream of a local variable or a parameter that the function also names outside the statement (it keeps it or hands it on), or whose value reads such a thing;
  • a stream of a dart:io object, such as a process’s output or a socket, which ends with it;
  • a listen given an onDone, which expects its stream to end: pass one where a stream ends by itself;
  • a class without dispose or close, a function outside a class, and tests.

Nothing can cancel a subscription that nothing holds. After the object is disposed or closed, the stream still calls it:

  • a ChangeNotifier notifies after dispose;
  • a State calls setState after it left the tree;
  • a Cubit emits after close, which throws a StateError.

dispose_owned_resources reports a subscription field that a State never cancels. This rule reports the subscription that is not even kept.

Violating
class ItemsCubit extends Cubit<List<Item>> {
ItemsCubit(ItemsRepository repository) : super(const []) {
repository.watchAll().listen(emit);
}
}
Corrected
class ItemsCubit extends Cubit<List<Item>> {
ItemsCubit(ItemsRepository repository) : super(const []) {
_subscription = repository.watchAll().listen(emit);
}
late final StreamSubscription<List<Item>> _subscription;
@override
Future<void> close() async {
await _subscription.cancel();
return super.close();
}
}

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Why it stays as written here.
// ignore: assay/subscription_never_cancelled

// ignore_for_file: assay/subscription_never_cancelled silences it in a whole file, and // ignore: subscription_never_cancelled without the prefix works too. See Suppressions.