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
What it reports
Section titled “What it reports”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 withtake; - a stream read from a field that the class releases, by calling
close,dispose,cancelor 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,terminateorshutdown. 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:ioobject, such as a process’s output or a socket, which ends with it; - a
listengiven anonDone, which expects its stream to end: pass one where a stream ends by itself; - a class without
disposeorclose, 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
ChangeNotifiernotifies afterdispose; - a
StatecallssetStateafter it left the tree; - a
Cubitemits afterclose, which throws aStateError.
dispose_owned_resources reports a subscription field that a State never cancels. This rule reports the subscription that is not even kept.
Example
Section titled “Example”class ItemsCubit extends Cubit<List<Item>> { ItemsCubit(ItemsRepository repository) : super(const []) { repository.watchAll().listen(emit); }}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(); }}Rule parameters
Section titled “Rule parameters”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. |
Suppressing it
Section titled “Suppressing it”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.