close_sinks
Close instances of dart:core
Sink
.
This rule is available as of Dart 2.0.
Details
#DO invoke close
on instances of dart:core
Sink
.
Closing instances of Sink prevents memory leaks and unexpected behavior.
BAD:
dart
class A {
IOSink _sinkA;
void init(filename) {
_sinkA = File(filename).openWrite(); // LINT
}
}
BAD:
dart
void someFunction() {
IOSink _sinkF; // LINT
}
GOOD:
dart
class B {
IOSink _sinkB;
void init(filename) {
_sinkB = File(filename).openWrite(); // OK
}
void dispose(filename) {
_sinkB.close();
}
}
GOOD:
dart
void someFunctionOK() {
IOSink _sinkFOK; // OK
_sinkFOK.close();
}
Known limitations
This rule does not track all patterns of Sink instantiations and closures. See linter#1381 for more information.
Usage
#To enable the close_sinks
rule,
add close_sinks
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- close_sinks
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。