Hive: Values in HiveList are lost

Created on 31 Jan 2020  路  2Comments  路  Source: hivedb/hive

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

  • Platform: Windows
  • Flutter version: N/A (Dart only)
  • Hive version: 1.3.0
documentation question

Most helpful comment

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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maxim-saplin picture maxim-saplin  路  3Comments

azilvl picture azilvl  路  3Comments

NourEldinShobier picture NourEldinShobier  路  3Comments

aminjoharinia picture aminjoharinia  路  3Comments

cachapa picture cachapa  路  4Comments