cancel_subscriptions
Cancel instances of dart:async
StreamSubscription
.
This rule is available as of Dart 2.0.
Details
#DO invoke cancel
on instances of dart:async
StreamSubscription
.
Cancelling instances of StreamSubscription prevents memory leaks and unexpected behavior.
BAD:
dart
class A {
StreamSubscription _subscriptionA; // LINT
void init(Stream stream) {
_subscriptionA = stream.listen((_) {});
}
}
BAD:
dart
void someFunction() {
StreamSubscription _subscriptionF; // LINT
}
GOOD:
dart
class B {
StreamSubscription _subscriptionB; // OK
void init(Stream stream) {
_subscriptionB = stream.listen((_) {});
}
void dispose(filename) {
_subscriptionB.cancel();
}
}
GOOD:
dart
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.
Usage
#To enable the cancel_subscriptions
rule,
add cancel_subscriptions
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- cancel_subscriptions
除非另有说明,文档之所提及适用于 Dart 3.5.3 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。