[英]Firestore how to get the collection value from another collection document id is referenced
[英]Flutter Dart with Firestore, How to get referenced document from one collection?
我创建了两个集合,如下所示:users - documentId(uid from auth) - fields and and sub collection (locations)
机会 - (自动生成的文档 ID) - 字段(其中一个字段是上述集合中的 locationDocumentId)
我正在检索机会作为 Stream,我在 Opportunity 类中创建了一个地图。
我希望在 Opportunity 类中有 Location 对象引用并在检索机会时填充对象。
我对 Firestore 和 Flutter 非常陌生,并被困在这里。
请告知我如何才能做到这一点。
谢谢你。
获取机会的代码
// ALL Opportunities
Stream<List<Opportunity>> get opportunities {
return _oppRef
.orderBy('lastUpdated', descending: true)
.snapshots()
.map(_allOpportunitiesFromSnapshot);
}
List<Opportunity> _allOpportunitiesFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return Opportunity.fromMap(doc.data, doc.documentID);
//HERE I WANTED TO ADD CODE TO GET LOCATION AS
//var opp = Opportunity.fromMap(doc.data, doc.documentID);
// opp.location ???
}).toList();
}
//
Future<DocumentSnapshot> getLocation(
String locationId, String createdBy) async {
return await _docRef
.document(createdBy)
.collection('locations')
.document(locationId)
.get();
}
我做了一些研究,我想结束这个,因为我真的需要重新设计我的数据模型。 我一直来自关系数据库设计,具有更多的规范化。 我已经研究过,用 NoSQL 做到这一点不是最佳实践(我也是 NoSQL 的新手)。 感谢 Renaud 的启蒙。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.