flutter - 如何使用 Flutter 从 Firestore 中获取最喜欢的数据
问题描述
我创建了以下方法,使该项目成为最喜欢的项目,如下所示:
void petFavorite(String petId) {
FirebaseFirestore.instance
.collection('pets')
.doc(petId)
.collection('favorites')
.doc(userModel.uId)
.set({
'favorite': true,
}).then((value) {
emit(AppGetFavoritesSuccessState());
}).catchError((error) {
emit(AppGetFavoritesErrorState(error.toString()));
});
}
这是获取fire Store中所有数据的以下方法:
List<String> petsId = [];
List<PetsModel> pets = [];
void getPetsData() {
FirebaseFirestore.instance.collection('pets').get().then((value) {
value.docs.forEach((element) {
petsId.add(element.id);
pets.add(PetsModel.fromJson(element.data()));
});
emit(AppGetPetsSuccessState());
}).catchError((error) {
emit(AppGetPetsErrorState(error.toString()));
});
}
这是以下型号.. 宠物型号:
class PetsModel {
String image;
String name;
String gender;
String age;
String petType;
String price;
String type;
String distance;
PetsModel({
this.image,
this.name,
this.gender,
this.petType,
this.price,
this.age,
this.type,
this.distance,
});
PetsModel.fromJson(Map<String, dynamic> map) {
image = map['image'];
name = map['name'];
gender = map['gender'];
age = map['age'];
petType = map['animal_type'];
price = map['price'];
type = map['type'];
distance = map['distance'];
}
Map<String, dynamic> toMap() {
return {
'name': name,
'image': image,
'gender': gender,
'animal_type': petType,
'age': age,
'price': price,
'type': type,
'distance': distance,
};
}
}
用户模型:
class UserModel {
String name;
String email;
String phone;
String uId;
String image;
String cover;
String bio;
bool isEmailVerified;
UserModel({
this.name,
this.email,
this.phone,
this.uId,
this.image,
this.cover,
this.bio,
this.isEmailVerified,
});
UserModel.fromJson(Map<String, dynamic> json) {
email = json['email'];
name = json['name'];
phone = json['phone'];
uId = json['uId'];
image = json['image'];
cover = json['cover'];
bio = json['bio'];
isEmailVerified = json['isEmailVerified'];
}
Map<String, dynamic> toMap() {
return {
'name': name,
'email': email,
'phone': phone,
'uId': uId,
'image': image,
'cover': cover,
'bio': bio,
'isEmailVerified': isEmailVerified,
};
}
}
我尝试了以下代码,但它带来了所有宠物:
List<PetsModel> favoritePets = [];
void getFavoritePets() {
emit(AppGetFavoritePetsLoadingState());
FirebaseFirestore.instance.collection('pets').get().then((value) {
value.docs.forEach((element) {
element.reference.collection('favorites').where('favorite', isEqualTo: true).get().then((value) {
favoritePets.add(PetsModel.fromJson(element.data()));
});
});
});
}
这个方法获取用户数据:
UserModel userModel;
getUserData() {
emit(AppGetUserLoadingState());
FirebaseFirestore.instance.collection('users').doc(uId).get().then((value) {
userModel = UserModel.fromJson(value.data());
emit(AppGetUserSuccessState());
}).catchError((error) {
print(error.toString());
emit(AppGetUserErrorState(error.toString()));
});
}
我只需要每个用户最喜欢的宠物..
这是我正在研究的正确逻辑吗?或者我必须在每个用户而不是每个宠物中保存这个收藏夹?
所以现在我想创建一个从下面的集合中获取收藏夹的方法,如下图所示:
这是以下用户数据:
解决方案
为了过滤您的数据,请尝试.where()
在方法之前运行查询 ( ) .get()
。只是想指出避免在您的帖子中共享私人信息。
推荐阅读
- python - 解码编码的 URL
- java - 通过Java每天根据预定时间自动备份MySql DB
- flutter - 如何在 Flutter 中绘制自定义背景?
- go - 为什么这个零长度和零容量切片不为零?
- javascript - React / Axios - “代码”:400,“消息”:“必需参数:部分”
- python - 读取大量图像时出现内存错误
- java - 动态 JPA 标准构建器
- python - 如何抓取 HTML 代码的不可见部分
- css - 如何修复 Chrome 和 Safari 之间的高度差?
- java - 整数可以是原始的,但 assertTrue 变得模棱两可——如何同时满足这两个要求?