Created
February 7, 2020 07:42
-
-
Save ajilo297/613e55e7ecea358f139e5ba89c8c93a1 to your computer and use it in GitHub Desktop.
A utility class that can hold values from Stream<List>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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