auto_disposal 0.0.1 copy "auto_disposal: ^0.0.1" to clipboard
auto_disposal: ^0.0.1 copied to clipboard

A Flutter package that provides a mixin to automatically dispose of common resources in StatefulWidgets, reducing boilerplate and preventing memory leaks.

auto_disposal #

auto_disposal is a small Flutter utility mixin that helps State classes clean up common resources automatically in dispose().

It reduces repetitive disposal code and keeps widget lifecycle cleanup in one place.

What It Supports #

AutoDisposalMixin can register and dispose/cancel these types:

  • StreamSubscription
  • AnimationController
  • FocusNode
  • TextEditingController
  • ScrollController
  • Ticker
  • OverlayEntry (remove + dispose)
  • Timer
  • StreamController
  • ChangeNotifier

It also supports custom cleanup with autoDisposeCallback(...).

Usage #

import 'dart:async';

import 'package:auto_disposal/auto_disposal.dart';
import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with AutoDisposalMixin<MyHomePage> {
  late final TextEditingController controller =
      autoDispose(TextEditingController());
  late final StreamController<int> streamController =
      autoDispose(StreamController<int>());

  @override
  void initState() {
    super.initState();
    autoDisposeCallback(() {
      // Any custom cleanup logic.
    });
  }

  @override
  Widget build(BuildContext context) {
    return TextField(controller: controller);
  }
}

Notes #

  • Registered callbacks are executed in reverse order during dispose().
  • Async cleanup callbacks are supported.
  • See the complete example in example/lib/main.dart.
1
likes
160
points
115
downloads

Documentation

API reference

Publisher

verified publisherbadrkouki.dev

Weekly Downloads

A Flutter package that provides a mixin to automatically dispose of common resources in StatefulWidgets, reducing boilerplate and preventing memory leaks.

Homepage

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on auto_disposal