簡體   English   中英

Flutter Dart with Firestore,如何從一個集合中獲取參考文檔?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM