Skip to content

proposal: always_dispose_change_notifier #58787

Open
@FMorschel

Description

@FMorschel

always_dispose_change_notifier

Description

Closing ChangeNotifiers prevents memory leaks and unexpected behavior.

Details

Closing ChangeNotifiers 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-proposallinter-status-pendingtype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions