【发布时间】:2021-01-04 17:23:13
【问题描述】:
我有点卡在我的项目中,以了解更多关于颤振的信息,希望你能帮助我或给我一些讲座的建议:)
我的 Firestore 数据库中有两个集合 - 一个用户和一个组集合。
在我的用户文档中,我有一个包含一些组 ID 的数组。 Groups 集合也非常简单 - 只有一些具有唯一 ID 的集合,并且作为字段只有名称和描述。
现在我想要一个列表,其中包含用户是成员的所有组 - 所以我必须获取一个包含组 ID 的列表,然后查询数据库以获取此 ID 的所有组数据
现在我正在取回组 ID 和组的正确数据。 但是如何将这些数据映射到 Listview 的 List 中?
我的代码 - 在主体的脚手架内调用 _buildBody。
Widget _buildBody() {
User _currentUser = FirebaseAuth.instance.currentUser;
DocumentReference docRef = FirebaseFirestore.instance.collection('user').doc(_currentUser.uid);
return StreamBuilder(
stream: docRef.snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if(snapshot.hasError){
return Error();
}
if(snapshot.connectionState == ConnectionState.waiting){
return Loading();
}
//Get all my Group Ids
final List<dynamic> documents = snapshot.data.get('groups');
Future<List<Map>> _groups = getGroups(documents);
//return ListView with Data
return ListView(...);
}
);
Future <List<Map<dynamic, dynamic>>> getGroups(List<dynamic> documents) async{
List<QueryDocumentSnapshot> group_docs;
List<Map<dynamic, dynamic>> list = new List();
QuerySnapshot collectionSnapshot = await FirebaseFirestore.instance.
collection('groups').where('id', whereIn: documents).get();
//Now I have all QueryDocumentSnapshots as a instance inside the list
group_docs = collectionSnapshot.docs;
//What now?! :D
return list;
}
非常感谢。
【问题讨论】:
标签: flutter google-cloud-firestore