问题描述
我正在使用 Flutter 制作应用,后端使用 Cloud Firestore。我有一个流,它检索所有用户的用户文档列表,并希望过滤最喜欢的食物是“意大利面”的用户。我不想加载其他文档。这是我的流,以及将它映射到我的用户模型的函数。
final CollectionReference usersCollection =
FirebaseFirestore.instance.collection('Users');``
List<MyAppUser> _userListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((DocumentSnapshot doc) {
return MyAppUser(
uid: doc.id ?? '',name: (doc['name']).toString() ?? '',email: (doc['email']).toString() ?? '',favorite_food: (doc['favorite food']).toString() ?? '',);
}).toList();
}
Stream<List<MyAppUser>> get users {
return usersCollection.snapshots().map(_userListFromSnapshot);
}
如果需要,这是我的用户模型:
class MyAppUser{
final String uid;
final String name;
final String email;
final String favorite_food;
MyAppUser({
this.name,this.email,this.uid,this.favorite_food,});
}
我应该在映射之后还是之前使用 where 函数?
如果我在映射之前过滤,我将不得不在原始流上做一个像
usersCollection.where('favorite food',isEqualTo: 'pasta')
如果我在映射后过滤,我可以获得类型安全:
我使用 Provider 收听流:final users = Provider.of<List<MyAppUser>>(context);
然后这样查询:
users.where((user) => user.favorite_food == 'pasta');
我更喜欢使用类型安全,但是,我是否需要为仅阅读过滤的文档或所有文档付费?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)