flutter - Flutter使用collectionGroup从firestore获取数据
问题描述
我正在创建一个应用程序,它将根据用户位置显示内容。为此,我使用 Geoflutterfire 包从 firestore 查询数据。当我使用单个文档字段时,应用程序会根据需要返回数据。例如
stream = radius.switchMap((rad) {
var collectionReference = firestore
.collection("content")
.doc("onecontentid")
.collection("allcontents");
return geo.collection(collectionRef: collectionReference).within(
center: center, radius: rad, field: 'position', strictMode: true);
});
但是我需要从“allcontents”子集合中流式传输所有文档,并尝试使用这样的collectionGroup来实现这一点
stream = radius.switchMap((rad) {
var collectionReference = firestore
.collectionGroup("allcontents");
return geo.collection(collectionRef: collectionReference).within(
center: center, radius: rad, field: 'position', strictMode: true);
});
它不返回任何数据。
我用于显示数据的流生成器如下所示
StreamBuilder(
stream: stream,
builder: (BuildContext context,
AsyncSnapshot<List<DocumentSnapshot>> snapshots) {
if (snapshots.connectionState == ConnectionState.active &&
snapshots.hasData) {
Do stuff}else {
return Center(child: CircularProgressIndicator());
对于第一种情况(单个文档查询),它能够从 firestore 检索数据(Do stuff) 对于第二种情况(collectionGroup),它只显示循环进度指示器。
解决方案
的GeoFlutterFire
collectionRef 将Query
类型作为输入,因此您的代码看起来不错并且应该可以工作。在处理streambuilder
结果时,您必须检查错误
if (snapshots.connectionState == ConnectionState.error) { // 错误状态 }
并在屏幕上更新结果。现在,对于除了成功之外的任何状态,它都被编码为显示进度指示器。
权限被拒绝错误的更新:
这可能是因为 Firestore 安全规则。我推荐此Firestore 安全规则参考,以便您可以编写更符合您的应用程序需求的安全规则。
推荐阅读
- docker - Docker compose 找不到构建路径
- macos - 正文请求的远程擦除 Intune 中的“MacOsUnlockCode”是什么?
- jquery - 如何在我自己的函数中使用 Dropzone.js
- r - 将整洁的 tibble 扩展到其原始形式
- jquery - 导入 jquery 插件在反应应用程序中不起作用
- spring-integration - 如何在带有 kafka 扩展的 spring-integration 中停止我的生产者容器/API
- java - 如何进行单元测试以检查日期时间的格式?
- logging - wxPython:创建日志系统
- c# - 正则表达式查找具有特定字符的单词
- bigcommerce - Bigcommerce 模板不适用于本地主机