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.
This package provides a custom lint rule that checks for:
-
Presenter Location (
presenter_instance_outside_state)- Ensures Presenter instances are only declared within State classes
- Prevents accidental usage of Presenters in non-State classes
-
Dispose Method Existence (
presenter_state_missing_dispose)- Verifies that State classes containing Presenter instances override the dispose method
- Helps maintain proper cleanup practices
-
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
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
- 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- Create or update your
analysis_options.yaml:
analyzer:
plugins:
- custom_lint
custom_lint:
rules:
- presenter_instance_outside_state
- presenter_state_missing_dispose
- presenter_not_disposedThe 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();
}
}-
Presenter Outside State
// ❌ Wrong: Presenter in non-State class class SomeClass { final presenter = MyPresenter(); }
-
Missing Dispose Method
// ❌ Wrong: No dispose method class _MyWidgetState extends State<MyWidget> { final presenter = MyPresenter(); }
-
Incomplete Disposal
// ❌ Wrong: Presenter not disposed class _MyWidgetState extends State<MyWidget> { final presenter = MyPresenter(); @override void dispose() { super.dispose(); // Missing presenter.dispose() } }
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.
This project is licensed under the MIT License - see the LICENSE file for details.