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

Time:11-12

I am trying to update my data in the real-time database in flutter for a web app. But whenever I update my data, it deletes all of the other fields in the same table.

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

Database Image

CodePudding user response:

Whenever you put data to a path in the database, the data you provide in the request replaces all existing data in that path.

If you want to only update a single property, you can two options:

  • You can put that single value to the lower level path:

    await put(
      Uri.parse(
        "https://officialnasproject-default-rtdb.firebaseio.com/App/Notification/NotificationChecked.json"),
      body: jsonEncode("YES"));
    }
    
  • You can use patch, which replaces only the keys you have in your NotificationData map and leaves other keys unmodified.

For more on these operations, I recommend reading the Firebase documentation on the REST API of the Realtime Database.

  • Related