cancel_subscriptions
Cancel instances of dart:async
StreamSubscription
.
Details
#DO invoke cancel
on instances of dart:async
StreamSubscription
.
Cancelling instances of StreamSubscription prevents memory leaks and unexpected behavior.
BAD:
class A {
StreamSubscription _subscriptionA; // LINT
void init(Stream stream) {
_subscriptionA = stream.listen((_) {});
}
}
BAD:
void someFunction() {
StreamSubscription _subscriptionF; // LINT
}
GOOD:
class B {
StreamSubscription _subscriptionB; // OK
void init(Stream stream) {
_subscriptionB = stream.listen((_) {});
}
void dispose(filename) {
_subscriptionB.cancel();
}
}
GOOD:
void someFunctionOK() {
StreamSubscription _subscriptionB; // OK
_subscriptionB.cancel();
}
Known limitations
This rule does not track all patterns of StreamSubscription instantiations and cancellations. See linter#317 for more information.
Enable
#To enable the cancel_subscriptions
rule, add cancel_subscriptions
under linter > rules in your analysis_options.yaml
file:
linter:
rules:
- cancel_subscriptions
If you're instead using the YAML map syntax to configure linter rules, add cancel_subscriptions: true
under linter > rules:
linter:
rules:
cancel_subscriptions: true
Unless stated otherwise, the documentation on this site reflects Dart 3.6.2. Page last updated on 2025-01-27. View source or report an issue.