【问题标题】:how to check if element exist in document field type array ? flutter如何检查文档字段类型数组中是否存在元素?扑
【发布时间】:2021-12-21 16:29:43
【问题描述】:

我有一组问题,其中包含用户喜欢这个问题的 id 字符串数组

我想检查这个数组中是否存在一个 id 然后显示一个小部件

List result = await FirebaseFirestore.instance
        .collection('QuestionBank')
        .doc(questionID)
        .snapshots()
        .map((event) => {List.from(event['userLiked'])})
        .toList();
    return result.contains(uid);
  }

这就是我在数组中添加项目的方式

Future<void> updateQuestionLikes(String uids, String userUid) async {
    await questionBank.doc(uids).set({
      'likes': FieldValue.increment(1),
      'userLiked': FieldValue.arrayUnion([userUid])
    }, SetOptions(merge: true));
  }

我想检查一个 uid 是否存在于 firestore 的 userLiked 字段中 不工作 有什么想法或解决方案可以提供帮助吗?

【问题讨论】:

    标签: arrays list firebase flutter google-cloud-firestore


    【解决方案1】:

    我想是这样的:

      Future<bool> ifUidExist(String uid)async {
        final query = await FirebaseFirestore.instance
            .collection("QuestionBank")
            .where("userLiked", arrayContains: uid)
            .get();
    
        return query.docs.isNotEmpty;
      }
    
    

    【讨论】:

    • 问题库集合包含很多问题,每个问题都包含自己的用户喜欢的数组字段我想知道集合中特定问题的真假
    • 但是查询它只在一个集合上。(您也可以检查文档是否存在)。如果您知道文档的路径,则需要下载所有文档并检查您想要的内容,就像您所做的那样“return result.contains(uid);”
    猜你喜欢
    • 2018-08-16
    • 2022-01-05
    • 2019-03-04
    • 1970-01-01
    • 2015-11-27
    • 2010-12-30
    • 2023-03-07
    • 1970-01-01
    • 2022-11-02
    相关资源
    最近更新 更多