Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values in HiveList are lost #208

Open
kaboc opened this issue Jan 31, 2020 · 2 comments
Open

Values in HiveList are lost #208

kaboc opened this issue Jan 31, 2020 · 2 comments
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@kaboc
Copy link

kaboc commented Jan 31, 2020

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
@kaboc kaboc added the problem An unconfirmed bug. label Jan 31, 2020
@simc
Copy link
Member

simc commented Jan 31, 2020

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.

@simc simc added documentation Improvements or additions to documentation question Further information is requested and removed problem An unconfirmed bug. labels Jan 31, 2020
@kaboc
Copy link
Author

kaboc commented Jan 31, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants