【问题标题】:Why is my data being removed when i update a certain field in a realtime database in flutter for web为什么当我在 Flutter for Web 中更新实时数据库中的某个字段时我的数据被删除
【发布时间】:2021-12-21 09:36:37
【问题描述】:

我正在尝试更新我在实时数据库中的数据,以更新 web 应用程序的颤振。但是每当我更新我的数据时,它都会删除同一个表中的所有其他字段。

ChangeNotificationStatus() async {
    Map NotificationData = {
      "NotificationChecked": "YES",
    };
    await put(
        Uri.parse(
            "https://officialnasproject-default-rtdb.firebaseio.com/App/Notification.json"),
        body: jsonEncode(NotificationData));
  }

【问题讨论】:

    标签: flutter http dart firebase-realtime-database web-applications


    【解决方案1】:

    每当您将put 数据指向数据库中的某个路径时,您在请求中提供的数据都会替换该路径中的所有现有数据。

    如果你只想更新一个属性,你可以有两种选择:

    • 你可以put那个单值到低层路径:

      await put(
        Uri.parse(
          "https://officialnasproject-default-rtdb.firebaseio.com/App/Notification/NotificationChecked.json"),
        body: jsonEncode("YES"));
      }
      
    • 您可以使用patch,它只替换您在NotificationData 映射中的键,而其他键保持不变。

    有关这些操作的更多信息,我建议阅读 REST API of the Realtime Database 上的 Firebase 文档。

    【讨论】:

      猜你喜欢
      • 2021-12-20
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多