首页 > 解决方案 > 在 Streambuilder 中的集合上使用 where() 不断返回 null

问题描述

我试图在集合中找到符合特定条件的所有文档,并将其保存在 Streambuilder 中,以便我可以使用它返回的所有文档中的数据创建一个表。但是我在 Streambuilder 中的流一直为我在 Flutter 中的查询返回 null。我在 Python 中尝试了相同的查询,它返回了正确的文档。在流构建器中不可能有 where 查询吗?作为参考,这是一个网络应用程序。

这是我的代码

class ContainerCustomerTable extends StatefulWidget {
  final company;

  ContainerCustomerTable({this.company});
  @override
  _ContainerCustomerTableState createState() => _ContainerCustomerTableState();
}

class _ContainerTableState extends State<ContainerCustomerTable> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance.collection(widget.company).where('some_criteria', isEqualTo: true).snapshots(),
        builder: (context, snapshot) {
          print(snapshot);
          QuerySnapshot docSnapshot = snapshot.data;
          if (docSnapshot != null) {
            return  Text('test1');
          } else{
            return LoadingIndicator();
          }

        }

    );

我正在打印快照只是为了检查它返回的内容,它给了我这个

AsyncSnapshot<QuerySnapshot>(ConnectionState.active, null, [cloud_firestore/unknown] NoSuchMethodError: invalid member on null: 'includeMetadataChanges')

因此它为该查询返回 null,但在 python 中的相同查询再次产生正确的结果。我究竟做错了什么?

作为记录,当我运行以下命令时,它可以工作:

FirebaseFirestore.instance.collection(widget.company).doc('some_doc').snapshots()

但由于某种原因,当我只是在集合或位置上运行快照时,我收到上面发布的错误

标签: fluttergoogle-cloud-firestoreflutter-web

解决方案


我想到了。我的语法很好,是 firestore js 包错误。如果您将 firestore 用于 Web 应用程序,请在您的 web/index.html 中使用此版本:

<script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-firestore.js"></script>

不是最后一个版本 8.0.2,当我降级时,一切都按预期工作。


推荐阅读