Map的Cloud Firestore查询在Flutter / Dart中返回空快照

时间:2020-07-01 20:52:37

标签: flutter dart google-cloud-firestore

我通过从firestore中检查Map键来获取文档的查询。我为此使用StreamBuilder。在Firestore和Flutter代码中,两个ID都相同。您知道为什么快照为空吗? postSnapshot.data.documents.length等于0,则返回[]。

StreamBuilder(
        stream: Firestore.instance
            .collection('posts')
            .orderBy('createdAt', descending: true)
            .where('userlist.' + widget.user.uid, isEqualTo: true)
            .snapshots(),
        builder: (context, postSnapshot) {
          if (postSnapshot.connectionState == ConnectionState.waiting) {
            return _displaysLoading();
          }
          if (postSnapshot.hasData) {
           ...
          }
        }
    )

enter image description here

2 个答案:

答案 0 :(得分:1)

您的查询未返回任何文档。这是因为您的数据没有布尔true作为地图的值。它具有“ user1”。使用isEqualTo: true的查询将永远不会将true与您在此处显示的字符串值匹配。

您将需要使查询与数据匹配并传递“ user1”,或者通过将值设为布尔值true使数据与查询匹配。

答案 1 :(得分:0)

尝试

StreamBuilder(
        stream: Firestore.instance
            .collection('posts')
            .orderBy('createdAt', descending: true)
            .where('userlist', arrayContains: widget.user.uid)
            .snapshots(),
        builder: (context, postSnapshot) {
          if (postSnapshot.connectionState == ConnectionState.waiting) {
            return _displaysLoading();
          }
          if (postSnapshot.hasData) {
           ...
          }
        }
    )
相关问题