Home > Back-end >  How to put data so that it does not disappear when using Hive typeadapter box
How to put data so that it does not disappear when using Hive typeadapter box

Time:04-01

I am using typeadapter box and having a trouble to put data now.

For example, when I create a "Person" class with name, age, and address fields and generated adapter,

The way to put data in the 'id1' key of 'Person box' is as follows. (assuming open and get the box)

put.box('id1', Person(name: 'john'));

Or like this.

put.box('id2', Person(name: 'aa', age: 10));

However, if you do the above, the previously stored values ​​of age or address(which are not saved fields in those code) appear to be deleted. Like 'set' method in Firestore without 'merge'

Is there a way to update only the 'name' value so that the 'age' and 'address' values ​​are not deleted like 'update' method in Firestore?

I have tried to find any method like update

CodePudding user response:

Here is an example from the documentation that shows how to update only the person's age.

var box = await Hive.openBox('myBox');

var person = Person()
  ..name = 'Dave'
  ..age = 22;

box.add(person);

print(box.getAt(0)); // Dave - 22

person.age = 30;
person.save();

print(box.getAt(0)) // Dave - 30

But before you update the Person you need to initialize it from the box and then update it. Maybe you update the Person with new data without initialized age or and the rest.

  • Related