Reputation: 93
I have a StreamBuilder feeding data to a list which is later displayed in a stack.
I want to filter (remove) certain cards from being added to the List[] by these rules:
I choose to do a StreamBuilder because I'd like to update the stack automatically after the user interacts with it (by liking it, it disappears and shows the next). For my understanding, a stream serves that purpose. But please correct me if I'm wrong and if there's a better approach.
streamCard() {
return StreamBuilder(
stream: postRef
.orderBy("timestamp", descending: true)
.limit(10)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return circularProgress();
}
List<PostCard> cards = [];
snapshot.data.documents.forEach((doc) {
cards.add(PostCard.fromDocument(doc));
});
If "cards" isn't empty:
return Stack(
alignment: Alignment.center,
children: cards,
In Firebase, I have: "Cards" > "cardID" > with "ownerId" and "liked" fields ("liked" contains every uid who has liked it).
The idea is to compare the current uid with the contents of each cardID. If they're present, dismiss that card and try the next.
I've been struggling with this problem for a while, I appreciate any help!
Upvotes: 1
Views: 1290
Reputation: 5819
Basically what you are trying to do is a logical OR
query on your firestore, which is very limited as described in the documentation. For comparison, with you where trying to make a logical AND
all you had to do it stack 2 where()
calls in your query.
So you can't simply limit this in the query itself (in this case, your stream
) but you can operate it in the stream's builder, which could be done doing something this:
builder: (context, snapshot) {
if (!snapshot.hasData) {
return circularProgress();
}
List<PostCard> cards = [];
snapshot.data.documents.forEach((doc) {
List<String> liked = List.castFrom(doc.get("liked"));
// currentUserId is previously populated
if(doc.get("ownerId") == currentUserId || liked.contains(currentUserId)){
cards.add(PostCard.fromDocument(doc));
}
});
return cards;
}
Upvotes: 1