【发布时间】:2021-04-17 21:11:08
【问题描述】:
我正在创建一个将根据用户位置显示内容的应用。为此,我使用 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时,您是否正在检查error状态?并手动检查 Firestore 中的数据。 -
请详细说明“您在检查错误状态吗?”因为我没有收到任何错误
-
if (snapshots.connectionState == ConnectionState.active)与此类似,您必须检查错误if (snapshots.connectionState == ConnectionState.error)并在屏幕上更新结果。现在,对于成功以外的任何状态,其编码都显示进度指示器。 -
谢谢,我收到一条错误消息“错误:Firestore:调用者没有执行指定操作的权限。(firestore/permission-denied)。”任何解决方案
-
更新了我的答案。它也可以为其他人提供解决方法!
标签: flutter geofirestore