简体   繁体   中英

get data from both collection and it is sub collection in firestore with flutter

i have this database structure in Firestore: 在此处输入图片说明 this what inside places在此处输入图片说明 how can i make a query in flutter to get all data inside Places sub-collection along with name from User collection, this code to get all places sub-collection data from every User collection :

Future<List<PlaceModel>> getPlaces() async {
    List<PlaceModel> placesList = [];
    // get all docs from user collection
    var users = await udb.get();
    for( var uid in users.docs) {
      var userData = await udb.doc(uid.id).get();
      var userPlaces = await udb.doc(uid.id).collection(placeTable).get();
      userPlaces.docs.forEach((place) {
            placesList.add(PlaceModel.fromMap(place.data()));
          });
    }
    return placesList;
  }

You can also use where() or orderby() methods to get data with some rules.

Firestore.instance
        .collection("users")
        .document(uid.id)
        .collection(placeTable)
        .where('fieldName', isEqualTo:name )
        .snapshots()

A single query can only access a single collection, or a group of collection that have the same name. Firestore queries are shallow and don't read data from subcollections. If you need the data from the subcollection, you will have to execute an additional query to read it.

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