dispose_controllers

SeverityQuick FixOptions
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();
  }
}

Usage

To enable the dispose_controllers rule, add dispose_controllers under custom_lint > rules in your analysis_options.yaml file:

custom_lint:
  rules:
    - dispose_controllers