Skip to content

Updating Single Object

Method

Future<Map> Backendless.data.of("TABLE-NAME").save(Map entity);
Future<E> Backendless.data.withClass<E>().save(E entity);

where:

Argument                Description
TABLE-NAME Name of the table where the object represented by Map will be updated. There must be objectId property in the map. The value of the property identifies the object which will be updated.
E Dart class of the data object to update.
entity Dart object to persist, must be of type E or Map (depending on the method used).

Return Value

The updated object of type E or Map.

Example

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
    });
  });
}

Consider the following class:

import 'package:backendless_sdk/backendless_sdk.dart';

@reflector
class Contact {
  String objectId;
  String name;
  int age;
  String phone;
  String title;
}
The following code saves a new instance of the Contact class and subsequently updates it:
void updateContact() {
  Contact contact = Contact()
    ..name = "Jack Daniels"
    ..age = 147
    ..phone = "777-777-777"
    ..title = "Favorites";

  Backendless.data.withClass<Contact>().save(contact).then((savedContact) {
    // now update the saved object
    savedContact.title = "Most favorite";
    savedContact.phone = "666-666-666";
    Backendless.data.withClass<Contact>().save(savedContact).then((response) {
      // Contact instance has been updated
    });
  });
}

Codeless Reference

data_service_saving_object

where:

Argument                Description
table name Name of the data table where a record must be updated.
object An object with properties that match the names of the table columns, these properties must contain new values for update operation. The objectId property and the corresponding value must be also included in the object.
return result Optional parameter. When this box is checked, the operation returns the updated object.

Returns the updated object.

Consider the following record stored in the employees data table:
data_service_example_update_2

The example below uses the objectId: "18AE9147-8016-412C-8E55-83B3188E153F" to find the record in the data table and update the value in the contactType column from "Personal" to "Work". To update more values in one query, specify the required number properties/column in the object and the new values.

data_service_example_update_1

The result of this operation will look as shown below after the Codeless logic runs:

data_service_example_update_3

Moreover, the operation described above has returned the updated object:

data_service_example_update_4