Recipes: DATABASE API

How to Save Objects With Relations and Create Dynamic Schema

I added the following method copied from the docs:

  void _saveOrder() {
    Map order = {
      "orderNumber": 1,
      "orderName": "Office Supplies",
    };
    Map orderItem1 = {
      "name": "Printer",
      "price": 99.0,
      "quantity": 1,
    };
    Map orderItem2 = {
      "name": "Paper",
      "price": 19.0,
      "quantity": 10,
    };

    Backendless.data.of("Order").save(order).then((savedOrder) {
      print("Order has been saved");

      Backendless.data.of("OrderItem").save(orderItem1).then((savedOrderItem1) {
        print("First order item has been saved");

        Backendless.data
            .of("OrderItem")
            .save(orderItem2)
            .then((savedOrderItem2) {
          print("Second order item has been saved");

          Backendless.data.of("Order").addRelation(
              savedOrder, "orderItems:OrderItem:n", children: [
            savedOrderItem1,
            savedOrderItem2
          ]).then((response) => print("Relation has been set"));
        });
      });
    });
  }

Backendless.data.of(“Order”).addRelation() has two compile time errors:

savedOrder.toString() fixes this one.

Map<dynamic, dynamic> savedOrder
The argument type ‘Map<dynamic, dynamic>’ can’t be assigned to the parameter type ‘String’.

Can’t do much about this one.

The named parameter ‘children’ isn’t defined.
Try correcting the name to an existing named parameter’s name, or defining a named parameter with the name ‘children’.

Future addRelation(
String parentObjectId,
String relationColumnName, {
List childrenObjectIds,
String whereClause,
}) // doesn’t use named parameters

I had a similar problem with Mission: API Services -CodelessShoppingCartService. I modified the downloaded code to fix the errors but it didn’t work.

I found a post that recommended:

  1. Upgrading to backendless_sdk: ^7.1.8
  2. Running flutter clean the flutter pub get

It worked.

I did the same for this code but it had no effect.

Thanks in advance
Chuck

Hello, @Chuck_H.

In order for your call to work correctly, you need to do the following:

  1. The first parameter must be a string (this is the objectId of your parent object). In your case, the correct parameter should look like this: saveOrder['objectId']
  2. What is this children parameter? The correct name is childrenObjectIds. And you should also add an objectId here, not an object.

I have corrected your code, it should look like this:

  void _saveOrder() async {
    Map order = {
      "orderNumber": 1,
      "orderName": "Office Supplies",
    };
    Map orderItem1 = {
      "name": "Printer",
      "price": 99.0,
      "quantity": 1,
    };
    Map orderItem2 = {
      "name": "Paper",
      "price": 19.0,
      "quantity": 10,
    };

    var savedOrder = await Backendless.data.of("Order").save(order);

    var savedOrderItem1 =
        await Backendless.data.of("OrderItem").save(orderItem1);

    var savedOrderItem2 =
        await Backendless.data.of("OrderItem").save(orderItem2);

    List<String> objectIds = [];
    objectIds.add(savedOrderItem1!['objectId']);
    objectIds.add(savedOrderItem2!['objectId']);

    Backendless.data
        .of("Order")
        .addRelation(savedOrder!['objectId'], 'orderItems:OrderItem:n',
            childrenObjectIds: objectIds)
        .then((response) => print("Relation has been set"));
  }

Best Regards, Nikita.

Thanks Nikita,

‘saved0rder’ is Map<dynamic, dynamic> and present in the downloaded code.

‘children:’ was present in the download source code.

I didn’t modify the code.

Could you check the website?

Thanks again,
Chuck

I didn’t understand your question. Just do as in the given example and everything will work without problems.

Hello Nikita,

This where I copied the code.

Flutter code:

I’ll use your solution.

Thanks

Nice!

Thanks Nikita!