首页 > 解决方案 > Flutter firebase 流有数据但返回 null

问题描述

我有一个问题,Firestore 中有一些文档具有相同的位置,并且在打印出正确的字符串时,位置被正确传递,但我的 StreamBuilder 总是在 if 条件下执行 noData 函数。

Streambuilder 看起来像这样:

StreamBuilder(
                      stream: FilterFirebase().relatedLocationFilter(
                          eventDocument["location"], eventDocument["id"]),
                      builder: (BuildContext ctx,
                          AsyncSnapshot<QuerySnapshot> snapshot) {
                        if (!snapshot.hasData ||
                            snapshot.data.docs.isEmpty) {
                          return NoDataRelatedLocation();
                        } else {
                          return new RelatedLocationListing(
                            relatedLocationList: snapshot.data.docs,
                          );
                        }
                      },
                    )

我的relatedLocationFilter 看起来像这样:

relatedLocationFilter(String location, String id) {
print(location);
print(id);
return FirebaseFirestore.instance
    .collection("events")
    .where("location", isEqualTo: location)
    .where("id", isNotEqualTo: id)
    .snapshots();
}

安全规则是正常的,没有 where 过滤器的代码工作正常。

更新:

日志:

颤振:Tj4EB68R1CwBYKPfWzcm

扑:米高梅宴会厅

颤振:1

Firestore 文档:

第一份文件

第二文件

第三份文件

标签: firebasefluttergoogle-cloud-firestorewhere-clausestream-builder

解决方案


这是正确的期望结果。因此,尽管您stream返回了正确的值,但流构建器在您stream获取数据之前首先获取 null。因此,如果您不想在获得真实数据NoDataRelatedLocation之前显示stream,则应放置initialData流构建器的参数。


推荐阅读