Reputation: 172
I created this stream that gets a snapshot of a specific document. Inside the document theres an array called tripAttractions that I want to build into a list. The question is, how do I access this specific array from the snapshot?
Stream<QuerySnapshot> getAttractions(BuildContext context) async* {
Firestore.instance
.collection('trips')
.document(documentId)
.snapshots(includeMetadataChanges: true);
}
The list shows how I'm trying to access the snapshot tripAttractions data but this doesn't work.
ListView.builder(
itemCount: snapshot.data['tripAttractions'].length,
itemBuilder: (BuildContext context, int index) =>
tripAttractionsCards(
context, index, snapshot.data['tripAttractions']),
);
Array inside the firestore document
Upvotes: 0
Views: 1069
Reputation: 172
Ok the issue was that incorrectly called QuerySnapshot. I'm supposed to use DocumentSnapshot since I'm calling a document directly. Also I had to add yield* to return the data from firestore.
Stream<DocumentSnapshot> getAttractions(BuildContext context) async* {
yield* Firestore.instance
.collection('trips')
.document(documentId)
.snapshots(includeMetadataChanges: true);
}
I was able to troubleshoot this by checking if the snapshot was sending any data by adding an if statement inside the streamBuilder.
if (!snapshot.hasData) return Text("Loading");
Upvotes: 0
Reputation: 599591
Most likely you're just missing the array accessor in the item builder:
ListView.builder(
itemCount: snapshot.data['tripAttractions'].length,
itemBuilder: (BuildContext context, int index) =>
tripAttractionsCards(
context, index, snapshot.data['tripAttractions'][index]),
);
Upvotes: 1