disposing 1.0.0+1 disposing: ^1.0.0+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: <LATEST_VERSION>
Flutter #
dependencies:
flutter_disposing: <LATEST_VERSION>
How to Use #
- First, you need to import disposing.dart file
import 'package:disposing/disposing.dart';
- Convert instance to disposable
// You can convert StreamSubscription, StreamController, Timer and so on.
final streamDisp = stream.listen((v) => {}).asDisposable();
final timerDisp = timer.asDisposable();
- (Optional) Add disposable instances to DisposableBag
final bag = DisposableBag();
bag.add(streamDisp);
bag.add(timerDisp);
Or you can add disposable directly
stream.listen((v) => {}).disposeOn(bag);
timer.disposeOn(bag);
- Dispose them!
// Without DisposeBag
await streamDisp.dispose();
await 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() {
Timer.periodic(Duration(seconds: 1), (t) => {}).disposeOn(disposeBag);
controller.addDisposableListener(() => print(controller.text)).disposeOn(disposeBag);
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);