I’m trying to display data from an array in firestore. I displayed it, but only [0] in the array is showing. I’m trying to get all the data in the array to show.
builder: (_, AsyncSnapshot<List<DocumentSnapshot>> snapshot){
if(snapshot.hasData){
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: ((_, index) {
List<Widget> tiles = [];
for (Map post in snapshot.data![index]['posts']) {
tiles.add(
Expanded(
child: Container(
margin: EdgeInsets.all(2),
padding: EdgeInsets.all(1),
decoration: BoxDecoration(border: Border.all(color:Colors.black)),
child: Center(
child: ListTile(
title: Text(post['postText'], style: TextStyle(color: Colors.white),),
subtitle: Text(post['fromUser'], style: TextStyle(color: Colors.white),),
),
),
),
)
);
}
return Expanded(
child: ListView(
children: tiles,
),
);
}),
);
}
else{
return Center(child: CircularProgressIndicator(),);
}
},
2
Answers
try this
Edit
To answer your qn about newest to oldest:
I suggest you put a
FieldValue.timestamp
field in your group chat documents! Then, you can order them like this:(All of that I copied by hand, since you hadn’t provided this code as text, as I asked you to!… 😆)
If you don’t have a timestamp field, there is a way to still find out when a document was created… but I don’t know how. Plus, in this case, I guess you want the time a certain FIELD was created in the document…! I don’t know if that’s possible. In fact, for that you’ll probably have to do:
Btw, if you want it to update when new messages come in, you can do like this:
Old answer:
But you’re telling it to display only post [0]!…
If there are more posts in each document, and you want to display all of them, you need to make a for-loop or something. For example:
And btw… Next time you ask a qn, plz paste your code as text rather than an image! So that we can copy-paste it into our answer, rather than having to retype it from the image. It’s so easy to make a mistake and then you get an error coz we didn’t copy it right.