I want a ListView with items from a Stream. Of course, content of the List should reflect changes in the Stream. And due to my designer quirks, I want items in the List be separated by a divider.
Just wondering, what is the proper way to create a ListView with separators, and reacting on Stream changes.
body: StreamBuilder(
stream: someStream,
builder: (ctx, snapshot) {
return ListView.separated(
separatorBuilder: (context, index) => Divider(color: Colors.black),
itemCount: ***whatsHere***?,
itemBuilder: (BuildContext ctx, int index) {
...
Hope I've missed something. Since an idea to get somehow the length of the source stream looks at least strange, because of the asynchronous nature of the streams. It seems might be feasible by StatefullWidget with stream subsriptions (and setState invoking), but StreamBuilder is invented to do exactly the same, isn't it?
snapshot
can have different states.
Generally you can do something like this:
if (snapshot.hasError){
//return error message
}
if (!snapshot.hasData){
//return a loader
}
//else you have data
List<your items> = snapshot.data;
// do your thing with ListView.builder
For more control you can check the snapshot.connectionsState
which can be none
,done
,active
,waiting
.
You can find more here for the AsyncSnapshot class and here for a quick tutorial