flutterobjectboxflutter-objectbox

Error 404 when saving object with relation in Objectbox after removing a ToMany relation


I use objectbox with Flutter for one of my project. I have two object link MyObject and InsideObject :

@Entity()
class MyObject {
  @Id()
  int id = 0;

  String? name;

  final insideObjects = ToMany<InsideObject>();

  MyObject({
    this.name,
  });

  void updateName(String? value) {
    if (value != null) {
      name = value.trim();
    } else {
      name = value;
    }
  }

  void addInsideObject(InsideObject insideObject) {
    insideObjects.add(insideObject);
  }

  void removeInsideObject(InsideObject insideObjectToRemove) {
    insideObjects
        .removeWhere((insideObject) => insideObject.id == insideObjectToRemove.id);
  }
}

and

@Entity()
class InsideObject {
  @Id()
  int id = 0;

  final holderObject = ToOne<MyObject>();
  final insideObject = ToOne<MyObject>();
  int quantity;

  InsideObject({
    this.quantity = 1,
  });
}

When I try to remove an insideobject in an object and then delete the InsideObject, when I call the function saveObject i get an Error 404 :

object.removeInsideGear(insideObject);
 await deleteInsideObject(insideObject);
 await saveObject(object);
Future<int> saveObject(MyObject object) async {
    final box = objectBox.store.box<MyObject>();
    return await box.putAsync(object); // here is the line that make the 404 error appear.
  }

with the delete funcion

 @override
  Future<bool> deleteInsideObject(InsideObject insideObject) async {
    final box = objectBox.store.box<InsideObject>();
    return await box.removeAsync(insideObject.id);
  }

if I try to do saveObject before deleteInsideObject it works

object.removeInsideGear(insideObject);
await saveObject(object);
await deleteInsideObject(insideObject);

but anyway i need to save the object when leaving the view so in my I will need to save the objectct after deleting the InsideObject.

I check with logs to see if the insideObject is correctly remove from the final insideObjects = ToMany<InsideObject>(); and it is : when I call the await saveObject(object); , insideObjects=[] so I don't see why there is a problem when trying to save the object after deleting the InsideObject linked.

If someone know what's happening it would be great to have your insight.


Solution

  • Alright, i found the issue. To avoid the problem I had to add a backlink to the ToMany property in order to make it work properly :

    @Entity()
    class MyObject {
    
    @Backlink('holderObject') // This was missing
    final insideObjects = ToMany<InsideObject>();
    
    MyObject({
     this.name,
    });
    
    ...
    }
    

    Here the foc info from objectbox :

    When using @Backlink it is recommended to explicitly specify the linked to relation using to. It is possible to omit this if there is only one matching relation. However, it helps with code readability and avoids a compile-time error if at any point another matching relation is added (in the above case, if another ToOne is added to the Order class).