user16330449
user16330449

Reputation: 60

How to put data so that it does not disappear when using Hive typeadapter box

I am using a type adapter box and having trouble putting data now.

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

The way to put data in the 'id1' key of the '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 that code) appear to be deleted. Like the 'set' method in Firestore without the 'merge'

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

I have tried to find any method like an update

Upvotes: 1

Views: 169

Answers (1)

Arnas
Arnas

Reputation: 832

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.

Upvotes: 1

Related Questions