实际上我想从 firebase 子集合中获取数据。
我想将子集合的数据提取到模型中。
实际上问题是它检索了超级集合的数据,但在子集合中却没有数据。
这是我的第一个模型
class Order{
Stream<List<OrderedFoodItem>> orderedFoodItemList; <--- Here OrderedFoodItem is Second Model
String name;
Order({this.orderedFoodItemList, this.name});
}
这是我的第二个模型
class OrderedFoodItem{
String foodItemName;
String foodItemPrice;
String foodItemQuantity;
OrderedFoodItem({this.foodItemName, this.foodItemPrice, this.foodItemQuantity});
}
第一个模型是外部模型,第二个模型是内部模型
这是我的代码,
Stream<List<Order>> get getPendingOrdersList {
String collectionPath = uid + "pendingorders";
final CollectionReference collectionReference = FirebaseFirestore.instance.collection(collectionPath);
return collectionReference.snapshots().map(_orderList);
}
List<Order> _orderList(QuerySnapshot querySnapshot) {
return querySnapshot.docs.map((document){
return Order(
name: document.get('name'),
orderedFoodItemList: FirebaseFirestore.instance
.collection(uid + "pendingorders")
.doc(document.id).collection(document.id).snapshots().map(_orderedFoodItemList),
);
}).toList();
}
List<OrderedFoodItem> _orderedFoodItemList(QuerySnapshot querySnapshot) {
return querySnapshot.docs.map((document) {
return OrderedFoodItem(
foodItemName: document.get('fooditem_name'),
foodItemPrice: document.get('fooditem_price'),
foodItemQuantity: document.get('fooditem_quantity'),
);
}).toList();
}
我正在获取数据的这段代码
StreamBuilder<List<Order>>(
stream: DatabaseService(uid: FirebaseAuth.instance.currentUser.uid).getPendingOrdersList,
builder: (context, snapshot){
if(snapshot.data != null){
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, outerIndex){
return Column(
children: [
Text(
snapshot.data[outerIndex].name,
),
StreamBuilder<List<OrderedFoodItem>>(
stream: snapshot.data[outerIndex].orderedFoodItemList,
builder: (context,innerSnapshot){
if(innerSnapshot.data != null){
return ListView.builder(
itemCount: innerSnapshot.data.length,
itemBuilder: (context, innerIndex) {
return Column(
children: [
Text(
innerSnapshot.data[innerIndex].foodItemName
),
Text(
innerSnapshot.data[innerIndex].foodItemPrice
),
Text(
innerSnapshot.data[innerIndex].foodItemQuantity
),
],
);
},
);
} else {
return CircularProgressIndicator();
}
},
),
],
);
},
);
}else{
return Text('no data');
}
},
),