【发布时间】:2022-12-10 21:36:38
【问题描述】:
我想更新 firebase flutter 应用程序中的文档。 但是数据库中可能不存在该文档,首先我想使用其中一个字段搜索文档,如果找到文档我想更新一个值,如果没有则什么也不会发生。
我有一个具有这些字段的用户模式。
final Map user;
final String mobile;
final String business_name;
final String business_address;
final String aadhar;
final String pan;
final bool isPhoneVerified;
final int growScore;
final String sector;
final String profile_picture;
final String business_picture;
final String uid;
final String about;
我想使用移动字段搜索文档并更新其 growScore。
这是我正在尝试但没有工作的
updateGrowScore(String phoneNumber, String post_type) async {
await ref
.watch(firestoreProvider)
.collection("my_users")
.where("mobile", isEqualTo: phoneNumber)
.get()
.then((value) => value.docs.map((e) {
ref
.watch(firestoreProvider)
.collection('my_users')
.doc(e.id)
.update({
'growScore': FieldValue.increment(post_type == "Bad" ? -5 : 5)
});
print("successfully updated");
}));
}
【问题讨论】: