Join us

Auto Disposal in Flutter Projects

8b60603d7f26f3795059b01838d85ee5


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!


Let's keep in touch!

Stay updated with my latest posts and news. I share insights, updates, and exclusive content.

Unsubscribe anytime. By subscribing, you share your email with @koukibadr and accept our Terms & Privacy.

Give a Pawfive to this post!


Only registered users can post comments. Please, login or signup.

Start writing about what excites you in tech — connect with developers, grow your voice, and get rewarded.

Join other developers and claim your FAUN.dev() account now!

Avatar

Kouki Badr

Mobile Developer, Nventive

@koukibadr
6 years grinding and translating ideas into engaging Mobile UIs with Flutter & Kotlin. My open-source packages are like free cheat codes for othes.
Developer Influence
21

Influence

1

Total Hits

5

Posts