title: Stop Writing dispose() by Hand — Introducing auto_disposal for Flutter
published: true
tags: flutter, dart, opensource, codeberg
Every Flutter developer has been there: you open a StatefulWidget, create a handful of controllers, and then... you have to remember to dispose every single one of them. Forget one, and you've silently introduced a memory leak.
auto_disposal is a tiny Flutter package I just published that solves this with a native Dart mixin — no dependencies, no code generation, no magic.
How it works
Add AutoDisposalMixin to your State class and wrap each resource with autoDispose(). That's it — everything registered is automatically disposed when the widget leaves the tree.
class _MyState extends State<MyWidget> with AutoDisposalMixin<MyWidget> {
late final controller = autoDispose(TextEditingController());
late final subscription = autoDispose(stream.listen((_) {}));
@override
void initState() {
super.initState();
autoDisposeCallback(() => print('custom cleanup!'));
}
}
No more overriding dispose(). No more forgetting.
Supported types
StreamSubscription, StreamController, AnimationController, TextEditingController, ScrollController, FocusNode, Ticker, Timer, OverlayEntry, ChangeNotifier, and custom callbacks.
Open source on Codeberg
The package is hosted on Codeberg — a community-owned, non-profit open source forge. I made the intentional choice to publish there instead of GitHub to support decentralized open source infrastructure.
- 📦 pub.dev: https://pub.dev/packages/auto_disposal
- 🌿 Codeberg: https://codeberg.org/koukibadr/auto_disposal
Contributions and feedback are very welcome!











