Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 颤振中火基条件下的过滤器列表_Firebase_Flutter_Dart_Google Cloud Firestore - Fatal编程技术网

Firebase 颤振中火基条件下的过滤器列表

Firebase 颤振中火基条件下的过滤器列表,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我有一个StreamBuilder,它将数据提供给一个列表,该列表稍后显示在堆栈中 我想通过以下规则过滤(删除)某些卡,使其不被添加到列表[]: 我是卡的所有者('ownerId'==currentUserId) 我喜欢这张卡('liked.'contains=currentUserId) 我选择使用StreamBuilder,因为我希望在用户与堆栈交互后自动更新堆栈(如果喜欢,它会消失并显示下一个)。据我所知,一条小溪就是为了这个目的。但是如果我错了,如果有更好的方法,请纠正我 strea

我有一个StreamBuilder,它将数据提供给一个列表,该列表稍后显示在堆栈中

我想通过以下规则过滤(删除)某些卡,使其不被添加到列表[]:

  • 我是卡的所有者('ownerId'==currentUserId)
  • 我喜欢这张卡('liked.'contains=currentUserId)
我选择使用StreamBuilder,因为我希望在用户与堆栈交互后自动更新堆栈(如果喜欢,它会消失并显示下一个)。据我所知,一条小溪就是为了这个目的。但是如果我错了,如果有更好的方法,请纠正我

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));
          });
在Firebase,我有: 带有“ownerId”和“liked”字段的“Cards”>“cardd”>“liked”>(“liked”包含所有喜欢它的uid)

其思想是将当前uid与每个cardID的内容进行比较。如果他们在场,就把那张卡扔掉,然后试试下一张

我已经为这个问题挣扎了一段时间,非常感谢您的帮助

编辑Firestore结构:

基本上,您要做的是对firestore进行逻辑的
查询,这是非常有限的,如中所述。为了进行比较,您尝试在何处进行逻辑
查询中的堆栈2
where()
调用

因此,您不能简单地将其限制在查询本身(在本例中,您的
)中,但您可以在流的生成器中对其进行操作,这可以通过以下方式完成:

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;
}
builder:(上下文,快照){
如果(!snapshot.hasData){
返回循环进程();
}
列表卡=[];
snapshot.data.documents.forEach((doc){
List liked=List.castFrom(doc.get(“liked”);
//currentUserId以前已填充
if(doc.get(“ownerId”)==currentUserId | | liked.contains(currentUserId)){
卡片。添加(明信片。从文件(doc));
}
});
回程卡;
}

谢谢您的回复!我实际上必须使用(owner!=currentuid | | |“liked”不包含)才能添加,有没有方法使用不包含?我尝试了代码,得到了
QueryDocumentSnapshot'没有实例方法'[]'。接收者:“QueryDocumentSnapshot”的实例尝试调用:[”(“ownerId”)
很抱歉,较新版本使用
document.get(field)
而不是
document[“field”]
,现在应该可以工作了,我不明白你的意思,使用方法不包含,但是我想你只需要改变if就可以做你想做的事情。修正了,谢谢!但是contains不起作用
类“\u InternalLinkedHashMap”没有实例方法“contains”。
将其改为
containsValue
,而不是
contains
,如果可以做到这一点,我会将其编辑到答案中。首先需要将列表转换为字符串列表,然后使用
contains(value)
,检查编辑后的答案,希望它现在可以工作:)
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;
}