简体   繁体   中英

Flutter Dart with Firestore, How to get referenced document from one collection?

I have created two collections as follows: users - documentId(uid from auth) - fields and and sub collection (locations)

opportunities - (auto generated document Id) - Fields (One of the field is locationDocumentId from above collection)

I am retrieving opportunities as Stream where I created a Map in Opportunity class.
I would like to have Location object reference in Opportunity class and populate the object while retrieving opportunities.

I am very new to Firestore and Flutter and got stuck here.

Please advise how I can accomplish this.

Thank you.

Code to get opportunities

  // 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();
  }

I have done some research and I would like to close this as I really need to redesign my data model. I've been coming from Relational database design with more normalization. I have studied that's it's not best practice to do that with NoSQL( I am totally newbie to NoSQL as well). Thank you Renaud for initiation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM