0
votes

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();
  }
1
Please share the code you have written for the opportunities.Renaud Tarnec
Hi Renaud, I have updated the question with the code. Thank you.Ravimaran

1 Answers

0
votes

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.