Open
Description
always_dispose_change_notifier
Description
Closing ChangeNotifier
s prevents memory leaks and unexpected behavior.
Details
Closing ChangeNotifier
s prevents memory leaks and unexpected behavior.
Kind
Groups: errors (just as close_sinks
and cancel_subscriptions
)
Good Examples
class B {
ChangeNotifier _changeNotifier;
void init(filename) {
_changeNotifier = TextEditingController(); // OK
}
void dispose(filename) {
_changeNotifier.dispose();
}
}
void someFunctionOK() {
ChangeNotifier _changeNotifier; // OK
_changeNotifier.dispose();
}
Bad Examples
class A {
ChangeNotifier _changeNotifier;
void init(filename) {
_changeNotifier = FocusNode(); // LINT
}
}
void someFunction() {
ChangeNotifier _changeNotifier; // LINT
}
Discussion
Since we already have close_sinks
and cancel_subscriptions
(and even always-remove-listener
on dcm) this should be added for Flutter users that instantiate FocusNode
or TextEditingController
(both are ChangeNotifier
) for example. Since the good practice is disposing it inside the current scope that should be just fine.
Mentioned here: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#be-explicit-about-dispose-and-the-object-lifecycle
Discussion checklist
- List any existing rules this proposal modifies, complements, overlaps or conflicts with.
- List any relevant issues (reported here, the SDK Tracker, or elsewhere).
- If there's any prior art (e.g., in other linters), please add references here.
- If this proposal corresponds to Effective Dart or Flutter Style Guide advice, please call it out. (If there isn’t any corresponding advice, should there be?)
- If this proposal is motivated by real-world examples, please provide as many details as you can. Demonstrating potential impact is especially valuable.