dispose_all

Creator: coderz1093

Last updated:

0 purchases

dispose_all Image
dispose_all Images

Languages

Categories

Add to Cart

Description:

dispose all

A Flutter package that introduces a convenient way to dispose of objects.
Features #

DisposableObject mixin. Makes any your custom class, or a class which was implemented in another package, usable (and disposable) with this package
disposeAll method for Iterable that disposes of every item of that Iterable. The elements can be:

DisposableObject, obviously
ChangeNotifier and anything that extends or uses it as a mixin, e.g. TextEditingController, FocusNode, ScrollController and much more
Sink, e.g. StreamController
StreamSubscription
Timer
AnimationEagerListenerMixin, e.g. AnimationController



Usage #
Disposing of a list of objects #
Define objects that must be disposed of later:
final textEditingController = TextEditingController();
final focusNode = FocusNode();
final stream = StreamController.broadcast();
final listener = someStream.listen(...);
final timer = Timer.periodic(...);
final animationController = AnimationController(...);
copied to clipboard
Put them all in an Iterable:
final disposables = [
textEditingController,
focusNode,
stream,
listener,
timer,
animationController,
];
copied to clipboard
Dispose of all objects when they are no longer needed:
disposables.disposeAll();
copied to clipboard
Make custom class disposable #
Custom classes instances cannot be disposed of by using disposeAll() method by default.
Luckily, there's an easy way to add such functionality to any class by DisposableObject mixin:
class DisposableTest with DisposableObject {
bool disposed = false; // or anything you need

@override
void disposeObject() {
disposed = true; // or anything you'd like to do
}
}
copied to clipboard
Now DisposableTest objects can be disposed of by disposeAll method:
final myDisposable = DisposableTest();
...
[
myDisposable,
...
].disposeAll();
copied to clipboard
Let's say you use Dio package for handling http requests. You might be using CancelToken objects to cancel http requests when you leave a page before http request is finished.
Calling disposeAll method on a list, where one of the elements is CancelToken will lead to throwing UnimplementedError. But you can easily make a wrapper for CancelToken to make things working:
import 'package:dio/dio.dart';

class CancelTokenDisposable extends CancelToken with DisposableObject {
@override
void disposeObject() {
cancel();
}
}
copied to clipboard
Now CancelTokenDisposable instances can be disposed of by disposeAll method:
class _MyWidgetState extends State<MyWidget> {
List _disposables = [];

CancelTokenDisposable cancelToken = CancelTokenDisposable();

@override
void initState() {
super.initState();
_disposables = [
cancelToken,
...
];
}

@override
Widget build(BuildContext context) {
...
}

@override
void dispose() {
_disposables.disposeAll();
super.dispose();
}
}
copied to clipboard
Contribution #
Please feel free to open an issue on GitHub, if you feel like this package misses some functionality required for your project.

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.