How to display a stream of list from a bloc using a listview How to display a stream of list from a bloc using a listview dart dart

How to display a stream of list from a bloc using a listview


Using rxdart's package streamcontrollers e.g BehaviourSubject you can consume your list Example in the bloc class:

final _list = BehaviourSubject<List<ChatMessage>>(); //private variableObservable<List<ChatMessage>> get list => _list.stream; //exposesvariable_publiclyexampleMethod(){ //prepare your list and when ready load the stream using sink_list.sink.add(readylist);}

Now on your listview use streambuilder

Widget _listView(){ return StreamBuilder(){   stream: blocProvider.list   builder: (context, AsyncSnapshot<List<ChatMessage>> listSnapShot){     //custom build your UI now with data in listSnapshot.data.   }

Hope this helps.