disposing 1.0.3+1 copy "disposing: ^1.0.3+1" to clipboard
disposing: ^1.0.3+1 copied to clipboard

Disposing is a flutter package adds dispose method on many classes.

disposing #

Disposing is a flutter package adds dispose method on many classes. It also provides Disposable and DisposableBag to easy to manage disposable instances.

Installation #

Add dependencies to your pubspec.yaml

Dart only #

dependencies:
  disposing: ^1.0.3+1

Flutter #

dependencies:
  flutter_disposing: ^1.0.3+1

How to Use #

  1. First, you need to import disposing.dart file
import 'package:disposing/disposing.dart';
  1. Convert instance to disposable
// You can convert StreamSubscription, StreamController, Timer and so on.
final streamDisp = stream.listen((v) => {}).asDisposable();
final timerDisp = timer.asDisposable();
  1. (Optional) Add disposable instances to SyncDisposableBag
final bag = DisposableBag();
bag.add(streamDisp);
bag.add(timerDisp);

Or you can add disposable directly

stream.listen((v) => {}).disposeOn(bag);
timer.disposeOn(bag);
  1. Dispose them!
// Without DisposeBag
await streamDisp.dispose();
timerDisp.dispose();

// With DisposeBag
await bag.dispose();

For Flutter #

flutter_disposing adds Listenable extension methods and DisposableBagStateMixin class.

Listenable

Listenable is a base class of ChangeNotifier which is used by TextEditingController, FocusNode, ValueNotifier and many other flutter classes.

Use addDisposableListener to adds a listener function and returns disposable instance.

final controller = TextEditingController();
final disp = controller.addDisposableListener(() => print(controller.text));

DisposableBagStateMixin

This mixin adds disposeBag variable and dispose it when the widget's state is being disposed.

class ExampleWidget extends StatefulWidget {
  ExampleWidget({Key? key}) : super(key: key);

  @override
  _ExampleWidgetState createState() => _ExampleWidgetState();
}

class _ExampleWidgetState extends State<ExampleWidget>
    with DisposableBagStateMixin {
  final controller = TextEditingController();

  @override
  void initState() {
    autoDispose(Timer.periodic(Duration(seconds: 1), (t) => {}).asDisposable());
    autoDispose(controller.addDisposableListener(() => print(controller.text)));
    autoDispose(controller.asDisposable());
    super.initState();
  }

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

using #

using is an utility method which will dispose disposable instance automatically after the callback execution is finished (like C# using statement).

await using(someDisposable, (disposable) async {
  // do something...
});
assert(someDisposable.isDisposed, true);
1
likes
110
pub points
65%
popularity

Publisher

verified publisherkscafe.work

Disposing is a flutter package adds dispose method on many classes.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

More

Packages that depend on disposing