【问题标题】:Flutter firebase stream has data but returns nullFlutter firebase 流有数据但返回 null
【发布时间】:2021-06-07 12:35:21
【问题描述】:

我有一个问题,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 文档:

【问题讨论】:

    标签: firebase flutter google-cloud-firestore where-clause stream-builder


    【解决方案1】:

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

    【讨论】:

    • 您好,这是有道理的,因为当我按下热重载时,它会显示一秒钟的数据。我会将什么作为 initalData 传递?
    • 你可以通过[]
    • 感谢您的帮助,但这样做时出现以下错误:无法将参数类型“StatelessWidget Function(BuildContext, AsyncSnapshot)”分配给参数类型“Widget Function” (BuildContext, AsyncSnapshot>)'
    • 这样是不是传递[]的正确方法:initialData:[],
    • 在这种情况下,您需要修改您的relatedLocationFilter 函数,使其不返回QuerySnapshot,而是将数组作为QuerySnapshot 的映射结果返回,并使用AsyncSnapshot&lt;List&lt;dynamic&gt;&gt;,而不是@987654331 @.
    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 2020-07-25
    • 2018-12-18
    • 2018-09-14
    • 1970-01-01
    • 2021-01-28
    • 2020-06-24
    • 2020-08-20
    相关资源
    最近更新 更多