Skip to content

seunghwanly/dart-custom-lint-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flutter Presenter Dispose Lint Rules

A custom lint rule package for Flutter that enforces proper disposal of Presenter instances in StatefulWidget's State classes. This package helps prevent memory leaks by ensuring Presenters are correctly disposed when the State is disposed.

Features

This package provides a custom lint rule that checks for:

  1. Presenter Location (presenter_instance_outside_state)

    • Ensures Presenter instances are only declared within State classes
    • Prevents accidental usage of Presenters in non-State classes
  2. Dispose Method Existence (presenter_state_missing_dispose)

    • Verifies that State classes containing Presenter instances override the dispose method
    • Helps maintain proper cleanup practices
  3. Proper Disposal (presenter_not_disposed)

    • Checks if all Presenter instances are properly disposed in the State's dispose method
    • Prevents memory leaks from undisposed Presenters

Quick Fixes

The package also provides automatic fixes:

  • Automatically adds a dispose method with proper Presenter disposal
  • Adds missing dispose calls for Presenter instances in existing dispose methods

Getting Started

  1. Add the package to your pubspec.yaml:
dev_dependencies:
  custom_lint: ^0.7.0
  winc_dart_custom_lint:
    git:
      url: https://github.com/your_username/winc_dart_custom_lint
  1. Create or update your analysis_options.yaml:
analyzer:
  plugins:
    - custom_lint

custom_lint:
  rules:
    - presenter_instance_outside_state
    - presenter_state_missing_dispose
    - presenter_not_disposed

Usage

The lint rules will automatically check your code. Here's an example of proper usage:

class MyPresenter extends Presenter {
  void doSomething() {
    // Presenter logic
  }
  
  @override
  void dispose() {
    // Cleanup resources
    super.dispose();
  }
}

class MyWidget extends StatefulWidget {
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  // ✅ Presenter instance in State class
  final MyPresenter _presenter = MyPresenter();

  @override
  void dispose() {
    // ✅ Properly dispose the presenter
    _presenter.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Common Issues and Fixes

  1. Presenter Outside State

    // ❌ Wrong: Presenter in non-State class
    class SomeClass {
      final presenter = MyPresenter();
    }
  2. Missing Dispose Method

    // ❌ Wrong: No dispose method
    class _MyWidgetState extends State<MyWidget> {
      final presenter = MyPresenter();
    }
  3. Incomplete Disposal

    // ❌ Wrong: Presenter not disposed
    class _MyWidgetState extends State<MyWidget> {
      final presenter = MyPresenter();
      
      @override
      void dispose() {
        super.dispose(); // Missing presenter.dispose()
      }
    }

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

using custom_lint package

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors