dispose-fields
preset: recommended
Warns when a widget state field is not disposed in dispose
method.
Not disposing fields that have a dispose
method may lead to memory leaks and should be avoided.
info
This rule will trigger for any field that has dispose
, close
or cancel
methods not called inside the widget's dispose
.
⚙️ Config
Set ignore-blocs
(default is false
) to ignore bloc fields. Enabling this option can be useful if you provide your blocs via BlocProvider
(and it closes the passed bloc automatically).
Set ignore-get-x
(default is false
) to ignore Rx classes from GetX.
dart_code_metrics:
...
rules:
...
- dispose-fields:
ignore-blocs: false
Example
❌ Bad:
class SomeDisposable implements Disposable {
void dispose() {}
}
class _ShinyWidgetState extends State<ShinyWidget> {
final _someDisposable = SomeDisposable();
final _anotherDisposable = SomeDisposable(); // LINT
void dispose() {
_someDisposable.dispose();
super.dispose();
}
}
✅ Good:
class SomeDisposable implements Disposable {
void dispose() {}
}
class _ShinyWidgetState extends State<ShinyWidget> {
final _someDisposable = SomeDisposable();
final _anotherDisposable = SomeDisposable();
void dispose() {
_someDisposable.dispose();
_anotherDisposable.dispose();
super.dispose();
}
}