Skip to content

Data Object

Backendless database stores objects in data tables. A persisted object is stored in a language-independent format. For the Flutter applications, there are two ways to represent data objects on the client side:

Map approach

Data objects can be stored/retrieved as instances of Map . For example, the following code saves an object with properties "name" and "age" in the "Person" table:

Map person = {"name": "Joe", "age": 25};
Backendless.data.of("Person").save(person);

Custom class approach

Data objects can be represented as instances of custom classes mapped to data tables. Object's class does not need to implement any special Backendless interfaces or extend a class from the Backendless SDK. For example, the following code saves an object with properties "name" and "age" in the "Person" table:

import 'package:backendless_sdk/backendless_sdk.dart';

@reflector
class Person {
  String name;
  int age;
}

Person person = Person()
  ..name = "Joe"
  ..age = 25;
Backendless.data.withClass<Person>().save(person);

Setting up

The Backendless Flutter SDK uses a reflectable plugin to process custom classes. We will need to complete the following steps before using custom classes:

  1. Add the build_runner dependency to your dev dependencies in the pubspec.yaml file:

    dev_dependencies:
     build_runner: ^1.0.0
    

  2. Next, in your main.dart file, add an import for the generated code and call the initializeReflectable() method in your main method. There might be an error stating that import doesn’t exist. Don’t worry, we will resolve it in the next step.

    import 'package:backendless_sdk/backendless_sdk.dart';
    import 'main.reflectable.dart'; // Import generated code
    
    main() {
      initializeReflectable(); // Set up reflection support.
      runApp(MyApp());
    }
    

  3. Open the terminal and run the following command:

flutter packages pub run build_runner build

This will generate all of the necessary code and resolve the import error.

Creating Custom Classes

There are several requirements imposed on the classes of the objects saved in the Backendless database:

  • The class should be annotated with @reflectorannotation. Don’t forget to add the following import to your class:

    import 'package:backendless_sdk/backendless_sdk.dart';
    

  • The class must contain the default, public, no-argument constructor.

  • The class must contain either public fields or getter and setter methods with the names that match names of the columns in the database.
  • Optional requirement - Backendless automatically assigns a unique ID to every persisted object. If the application needs to have access to the assigned ID, the class must declare the following field:

    String objectId;
    

  • Optional requirement - in addition to objectId, Backendless maintains two other properties for every persisted object - created and updated. The former contains the timestamp when the object was initially created in the Backendless database. The latter is updated every time the object is updated. To get access to these values, the class must declare the following fields:

    DateTime created;
    DateTime updated;
    

Here is a sample "Person" class:

import 'package:backendless_sdk/backendless_sdk.dart';

@reflector // This annotation enables reflection on Person
class Person {
 String name;
 int age;

 // We don't have to create no-arg constructor here, because if we don't specify a constructor,
 // the default no-argument constructor will be created.
}

Usage

If you want to use the “Person” class in your main.dart file, add an import for this class:

import 'person.dart';

Then run the following command in the terminal again:

flutter packages pub run build_runner build

Remember to run this command each time you import a new custom class or make changes to an existing custom class! It will update the generated code for new changes.

After you create the custom class, add an import for it and run the command. You can use the Backendless.data.withClass<T>() method to work with your Backendless database via custom classes.

The following code saves an object with properties “name” and “age” in the “Person” table:

Person person = Person()
   ..name = "Joe"
   ..age = 25;
Backendless.data.withClass<Person>().save(person).then((savedPerson) {
   print("The object has been saved to the database");
});