How to Use Custom Classes in the Backendless Flutter SDK

by on October 3, 2019

Custom Classes In Backendless Flutter SDK

We are pleased to introduce the new version of the Backendless Flutter SDK, v0.2.0, that adds support for custom classes. In this article, we are going to demonstrate how to start working with it.

To start, open your Flutter project or create a new one and add the Backendless Flutter plugin to it. You can find detailed instructions on how to get started with Backendless Flutter SDK here.

Setting Up The Flutter SDK

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:
     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.
  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

Now let’s declare the custom class, create the object of this class and save it in the Backendless database. There are several requirements imposed on the classes of the objects saved in the Backendless database:

  • The class should be annotated with @reflector annotation. 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 public fields with the names that match the 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.

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 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() = "Joe"
   ..age = 25;<Person>().save(person).then((savedPerson) {
   print("The object has been saved to the database");

That is all! Thanks for reading and Happy Coding with Backendless!

Leave a Reply