问题描述
我正在创建一个应用,该应用将根据用户位置显示内容。为此,我使用 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,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 检索数据(做东西)对于第二种情况(collectionGroup),它只显示循环进度指示器。
解决方法
GeoFlutterFire
的 collectionRef 将 Query
类型作为输入,因此您的代码看起来不错并且应该可以工作。在处理 streambuilder
结果时,您必须检查错误
if (snapshots.connectionState == ConnectionState.error) { // 错误状态 }
并更新屏幕中的结果。现在,对于除成功以外的任何状态,其编码以显示进度指示器。
更新权限被拒绝错误:
这可能是因为 Firestore 安全规则。我推荐此 Firestore Security Rule 参考,以便您可以编写更安全的规则来满足您的应用程序需求。