dispose_controllers
Severity | Quick Fix | Options |
---|---|---|
Error | ✅ | ❌ |
Details
DO dispose AnimationController
and all ChangeNotifier
subclasses such as TextEditingController
, ScrollController
and TabController
.
Controllers should be disposed in Widget
's dispose
method to avoid memory leaks.
This lint supports custom controllers that extend
ChangeNotifier
.Bad
class _MyWidgetState extends State<MyWidget> {
final _textEditingController = TextEditingController();
@override
Widget build(BuildContext context) {
return Placeholder();
}
}
Good
class _MyWidgetState extends State<MyWidget> {
final _textEditingController = TextEditingController();
@override
Widget build(BuildContext context) {
return Placeholder();
}
@override
void dispose() {
_textEditingController.dispose();
super.dispose();
}
}