为什么我在 flutter for web 中更新实时数据库中的某个字段时我的数据被删除

Why is my data being removed when i update a certain field in a realtime database in flutter for web

我正在尝试在 flutter 中为 Web 应用程序更新实时数据库中的数据。但是每当我更新数据时,它都会删除同一 table.

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

每当您 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 文档。