Duplicating the same data when writing to the database

Hello! I’m completely new to development and flutter as well. Please tell me how to implement the simultaneous function of recording and updating data in the database?

For example, I have text forms, the user enters information there and clicks the save button, Backendless saves the data to the database, but if the user clicks the save button again with the same data, then Backendless will save a new row of data in the database, and I need to he replaced the old values ​​with the new ones. The update method described in the documentation Updating Single Object - Backendless SDK for Flutter API Documentation is not suitable. I need that by clicking on one button, information is added to the database and update, without duplicating rows.

Sorry for my english. I hope I explained it clearly.

Since I’m a beginner, I will be glad if you show me an example code and guide me on the right path.

1 Like

Hi Viktor,

Welcome to our community. When you save the object initially, the response for that operation will return the object that has been “enriched” with an objectId value assigned to it. For the subsequent operation you should use that object and the same save operation. Backendless will check if objectId is present and in that case will perform an update operation.

Regards,
Mark

1 Like

Hi @mark-piller Can you please give an example of the code, I tried to figure it out myself, but so far it turns out badly

Hi @Viktor_Volodin,

I am here to help you. Sample code of what exactly do you want to see? How to update an object is pretty well shown in the documentation.

void updateContact() {
  // create new contact object first. Then we will update it.
  Map contact = {
    "name": "Jack Daniels",
    "age": 147,
    "phone": "777-777-777",
    "title": "Favorites",
  };

  Backendless.data.of("Contact").save(contact).then((savedContact) {
    savedContact["title"] = "Most favorite";
    savedContact["phone"] = "666-666-666";
    Backendless.data.of("Contact").save(savedContact).then((response) {
      // Contact object has been updated
    });
  });
}

If you are looking for a way to implement this inside your application logic, then unfortunately this is outside the scope of our support, I would recommend using Stackoverflow for such questions.

Cheers,
Stanislaw

1 Like

Hi! @stanislaw.grin Thanks for the answer! I used this update code, but it still creates extra rows when saving. I recorded a video where it is roughly clear what I mean. I need him to overwrite the new data (which he took from the text form) into the old row

Video about the problem - https://youtu.be/u2Q95j2Dhv0

Thanks for helping me!

I see that you call updateContact twice, but this is wrong, that’s why it does not work for you.
What you should do in your code logic is basically:

  1. When you create a new object in the database, set the result to some variable, let’s call it savedCar
  2. When the user changes data in form, apply these changes to the savedCar variable (which still holds object from the server)
  3. After the user saves data, this time you should send savedCar variable.

The code I have provided you in the previous message was just a show-case, not the implementation of your logic.
Hope this helps!

Cheers,
Stanislaw

1 Like

If you are about the fact that I clicked the “Save” button two times, then it is conceived, the user can enter data in one form, and click save, but not exit, and after a while enter the data in another form, or change the old data and then press the save button again and everything should be written in one line. (If you are talking about the code above the updateContact method, then it is not used)

I cannot use one button to save and the other to update

As for the variables, I already have the text stored in such variables avto_Model.text and avto_gosnomer.text

Thank you!

I’m not talking about pressing a button, it goes without saying. I’m talking about the logic that is executed when the button is pressed. On the first click, you must remember the result in a variable somewhere, and on subsequent clicks, use this result to update the data in the database. The main point here is that for all subsequent calls to the server, the sent object must contain the objectId field, taken from the result of the first call.

Honestly, it’s not very successful yet, but I’m trying, thanks!