0 purchases
disposing
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
copied to clipboard
Flutter #
dependencies:
flutter_disposing: ^1.0.3+1
copied to clipboard
How to Use #
First, you need to import disposing.dart file
import 'package:disposing/disposing.dart';
copied to clipboard
Convert instance to disposable
// You can convert StreamSubscription, StreamController, Timer and so on.
final streamDisp = stream.listen((v) => {}).asDisposable();
final timerDisp = timer.asDisposable();
copied to clipboard
(Optional) Add disposable instances to SyncDisposableBag
final bag = DisposableBag();
bag.add(streamDisp);
bag.add(timerDisp);
copied to clipboard
Or you can add disposable directly
stream.listen((v) => {}).disposeOn(bag);
timer.disposeOn(bag);
copied to clipboard
Dispose them!
// Without DisposeBag
await streamDisp.dispose();
timerDisp.dispose();
// With DisposeBag
await bag.dispose();
copied to clipboard
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));
copied to clipboard
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,
);
}
}
copied to clipboard
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);
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.