简体   繁体   中英

Flutter FireStore ,Try to get array of collection from array of document by one request

I have an application under construction I want to access, in FIRESTORE I have several countries as a doc and within each country I have a collections and inside the collections I have a list of doc I need access to all.

Example:

在此处输入图像描述

My Code:

Stream<List<RecentChat>> getFavo({userId , countryName}) { 
    List<CountryModel> country;
    for(CountryModel c in countryName) {
        var ref = _db.collection('country').doc(c.countryName).collection('chat').where("likes", arrayContains: userId).snapshots(); 
        return ref.map((snap) => snap.docs.map((doc) => RecentChat.fromJson(doc.data())).toList());
    }
}

Current Behavior: Only the first doc is returned.

Desired Behavior: Return all the docs.

Yes indeed only the first element will be returned, since you are doing a return in side of the loop and not using the Stream keyword yield .

Example:

Stream<List<RecentChat>> getFavo({userId , countryName}) { 
    List<CountryModel> country;
    for(CountryModel c in countryName) {
        var ref = _db.collection('country').doc(c.countryName).collection('chat').where("likes", arrayContains: userId).snapshots(); 
        yield ref.map((snap) => snap.docs.map((doc) => RecentChat.fromJson(doc.data())).toList());
    }
}

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