【问题标题】:how to search document and update it in firebase flutter如何在 firebase flutter 中搜索文档并更新它
【发布时间】: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");
            }));
  }

【问题讨论】:

    标签: flutter firebase crud


    【解决方案1】:

    如果文档不存在,那么当您在查询上调用 get() 时,它将返回一个空列表,因此如果不存在文档,您将 map() 一个空列表,这样什么都不会发生。

    【讨论】:

      猜你喜欢
      • 2020-05-24
      • 2020-03-19
      • 2021-05-17
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2021-12-15
      • 2019-10-06
      • 2019-06-14
      相关资源
      最近更新 更多