对于我在Flutter中构建的应用程序,我试图从Firestore检索登录用户在不同日期的保留时隙列表。我到达了可以在给定日期检索保留时隙的位置(通过对该日期进行硬编码并将其传递给futurebuilder),但是当我尝试从collectionGroup检索数据时,列表视图保持为空(快照没有数据)。现在,作为Flutter的初学者,我发现很难理解如何处理collectionGroup查询,因此,我们将不胜感激!
为了快速概述我的数据库的结构,我在github上提供了两张图片:
下面我包括了futurebuilder和listview构建器
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class TimesTest extends StatefulWidget {
@override
_TimesTestState createState() => _TimesTestState();
}
class _TimesTestState extends State<TimesTest> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: MyFutureBuilder(),),);
}
}
class MyFutureBuilder extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: MyTimes().getMyTimes('${loggedInUser.email}'),builder: (context,Asyncsnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueaccent,);
} else if (snapshot.hasData) {
return ListView.builder(
padding: EdgeInsets.symmetric(horizontal: 10.0,vertical: 20.0),itemCount: snapshot.data.documents.length,itemBuilder: (BuildContext context,int index) {
Documentsnapshot user = snapshot.data.documents[index];
return Padding(
padding: EdgeInsets.symmetric(
horizontal: 7.0,vertical: 3.0,child: Column(
crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
Card(
color: Colors.white,child: Container(
child: ListTile(
onTap: () {},title: Text(
user.data['hour'],style: TextStyle(
color: Colors.grey[900],subtitle: Text(
user.data['reserved'],leading: Icon(
Icons.access_time,color: Colors.grey[900],],);
});
} else if (snapshot.connectionState == ConnectionState.done &&
!snapshot.hasData) {
return Center(
child: Text('No times found'),);
} else {
return Center(
child: Text('Something else is wrong'),);
}
},);
}
}
这就是我从Firestore获取数据的方式。
import 'package:cloud_firestore/cloud_firestore.dart';
class MyTimes {
Future getMyTimes(String reserved) async {
return Firestore.instance
.collectionGroup('hours')
.where('reserved',isEqualTo: reserved)
.getDocuments();
}
}
我创建了一个索引,其集合ID为“ hours”,集合范围为“ collectionGroup”,现在我的安全规则如下:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read,write: if true;
}
match /{path=**}/hours/{document}{
allow read,write;
}
}
}
我已经花了很多小时试图找出答案,所以真的很感谢任何有用的东西!