Hi StackOverflow Community,
I am trying to access values from my Firestore database located in two different collections.
- I could retrieve data from the first collection through a StreamBuilder without any problems.
- However, I cannnot retrieve data from the second collection. I want to get the data through a where query within a second StreamBuilder but I can't access the values... I only get StreamBuilder<QuerySnapshot<Object?>> returned.
How can I also access values from my second collection as well? Thank you in advance!
Here is my code:
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot> (
stream: _firestore
.collection('customers')
.doc(user!.uid)
.collection('cart')
.snapshots(),
builder: (context, snapshot){
if(snapshot.hasData){
final cart = snapshot.data!.docs;
List<Column> cartWidgets = [];
List<String> ids = [];
for (var product in cart) {
final productCount = product['count'];
final productID = product['id'];
final productPrice = StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('product')
.where('id', isEqualTo: productID)
.snapshots(),
builder: (context, snapshot) {
if(!snapshot.hasData){
return Text('Something went wrong...');
} else{
final products = snapshot.data!.docs;
final productPrice = products.first['price'];
return Text(productPrice.toString());
}
}
);
final productWidget = Column(
children: [
Text('Quantity: $productCount'),
Text('ProductID: $productID'),
Text('Price: $productPrice'),
SizedBox(height: 50,)
]
);
cartWidgets.add(productWidget);
}
return SingleChildScrollView(
child: SafeArea(
child: Column(
children: cartWidgets,
),
),
);
} else{
return const Center(
child: const CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
}
}
);
}
Emulator: Emulator
Firestore: First Collection: Works without problems
Second collection: Cannot access data from here as second source