Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
623 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...