在Flutter中查询Firestore地图字段 [英] Querying Firestore map fields in flutter

查看:44
本文介绍了在Flutter中查询Firestore地图字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从名为书签" 的字段中检索Firestore数据库中的帖子.字段书签" 的类型为map.当用户为帖子添加书签时,该字段会更新,将 deviceId 设置为 true ,如下所示:

I am trying to retrieve posts in my Firestore database from a field called "bookmarks". The field "bookmarks" is of type map. When a user bookmarks a post, the field is updated with the deviceId being set to true like this:

▼ bookmarks
     deviceId : true

帖子被取消书签后, deviceId:true 被删除,并且 bookmarks 字段保持空白,如下所示:

When the post is unbookmarked, deviceId : trueis deleted and the bookmarks field remains empty like this:

书签:{}

我正在尝试从 deviceID 设置为 true 书签" 中检索帖子.但是,我的查询返回了所有帖子,包括 bookmarks 为空的帖子.

I am trying to retrieve posts from "bookmarks" where deviceID is set to true. My query however returns all posts including posts where bookmarks is empty.

这是我的查询:

QuerySnapshot snapshot = await postsRef
        .document(postId)
        .collection('users_posts')
        .where('bookmarks', arrayContains: deviceId)
        .orderBy('timestamp', descending: true)
        .getDocuments();

请帮助弄清楚我在做什么错.被吸引的是我的数据库的结构.谢谢.

Please help figure out what I am doing wrong. Attatched is the structure of my database. Thank you.

推荐答案

您的书签字段不是数组,因此您不能像数组一样查询它.这是一张地图,您可以使用点符号查询地图的嵌套字段的值:

Your bookmarks field is not an array, so you can't query it like an array. It's a map, and you can query for the values of nested fields of a map using dot notation:

.where('bookmarks.${deviceId}', isEqualTo: true)

这篇关于在Flutter中查询Firestore地图字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆