Steps to Reproduce
See below.
Code sample
This is an extract from the Relationships section of hive docs.
void main() async {
Hive.registerAdapter(PersonAdapter());
var persons = await Hive.openBox<Person>('personsWithLists');
persons.clear();
var mario = Person('Mario');
var luna = Person('Luna');
var alex = Person('Alex');
persons.addAll([mario, luna, alex]);
mario.friends = HiveList(persons); // Create a HiveList
mario.friends.addAll([luna, alex]); // Update Mario's friends
print(mario.friends);
luna.delete(); // Remove Luna from Hive
print(mario.friends); // HiveList updates automatically
}
The last line outputs [alex], meaning HiveList (mario.friends) holds a value in it.
I thought the value would be stored in the .hive file and the next code would still output [alex], but actually resulted in [].
void main() async {
Hive.registerAdapter(PersonAdapter());
var persons = await Hive.openBox<Person>('personsWithLists');
var mario = persons.getAt(0);
mario.friends = HiveList(persons);
print(mario); // Mario
print(mario.friends); // []
}
Is it an expected behaviour?
Commenting out the line of mario.friends = HiveList(persons); in the second code did not affect the result.
Version
This is expected behavior. You need to call mario.save() after mario.friends.addAll() in order to persist the change.
Later, after you call luna.delete() it is not necessary to call mario.save() again because Hive knows that luna does not exist any more.
I will add a small example to the docs.
I added mario.save() and it worked as expected. Thank you!
A new small example as you have mentioned will definitely be helpful for other people too.
Most helpful comment
This is expected behavior. You need to call
mario.save()aftermario.friends.addAll()in order to persist the change.Later, after you call
luna.delete()it is not necessary to callmario.save()again because Hive knows thatlunadoes not exist any more.I will add a small example to the docs.