How to update data in objectbox flutter

Issue

I was using ObjectBox for storing data. But how to update the same data. In documentation, it says it will update the data. But When I pass an old note with some new details, it create new note.

 Future<int> createNotes(Note note) async {
    var store = await BoxStore.getStore();
    var box = store.box<Note>();
    int id = box.put(note);
    print('note id: $id');
    store.close();
    return id;
  }

Thanks

Solution

The code you provide looks like creating a new note.

ObjectBox updates a record if it finds the field id in its internal table key, otherwise it creates a new record. So either you store the note id inside Note note when getting the note from ObjectBox and use it when updating, or if you are using some kind of an own id, then – upon storing – first get the ObjectBox internal id by creating a query on your own id and use this id to put the note as an update

Answered By – w461

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.