我创建了两个集合,如下所示:用户 - documentId(来自 auth 的 uid) - 字段和子集合(位置)
机会 - (自动生成的文档 ID) - 字段(其中一个字段是来自上述集合的 locationDocumentId)
我正在以 Stream 的形式检索机会,其中我在 Opportunity 类中创建了一个 Map。
我想在 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();
}