Skip to content

Instantly share code, notes, and snippets.

@ajilo297
Created February 7, 2020 07:42
Show Gist options
  • Save ajilo297/613e55e7ecea358f139e5ba89c8c93a1 to your computer and use it in GitHub Desktop.
Save ajilo297/613e55e7ecea358f139e5ba89c8c93a1 to your computer and use it in GitHub Desktop.
A utility class that can hold values from Stream<List>
import 'dart:async';
class StreamedList<T> {
StreamController<List<T>> _controller = StreamController.broadcast();
Stream<List<T>> get stream => _controller.stream;
List<T> get dataList => this._list;
List<T> _list = [];
void removeFromList(T value) {
List<T> dataList = List.generate(
this.dataList.length,
(index) => this.dataList.elementAt(index),
growable: true,
);
dataList.remove(value);
updateList(dataList);
}
void updateList(List<T> list) {
_list = list;
_dispatch();
}
void addToList(T value) {
_list = [..._list, value];
_dispatch();
}
void _dispatch() {
_controller.sink.add(_list);
}
void dispose() {
_list = null;
_controller.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment