isar / hive

Lightweight and blazing fast key-value database written in pure Dart.
Apache License 2.0
4.02k stars 402 forks source link

Can't add to HiveList if HiveObject is in inner collection #632

Open subzero911 opened 3 years ago

subzero911 commented 3 years ago

Steps to Reproduce

I want to store a box where key is user ID and value is List of Wallet (every Wallet is a HiveObject). I also want a user to have a HiveList of its wallets. The problem is adding to HiveList requires to check if HiveObject is inside of box. And when I add to box not a HiveObject directly, but a List<HiveObject>, the box property of every HiveObject is null!

Desired behaviour: if I put a collection into box, every element should also have a reference to this box in its box property

Code sample

Box<List<CryptoWallet>> walletsBox;
walletsBox = await Hive.openBox<List<CryptoWallet>>('wallets');

user.wallets = HiveList(walletsBox);

var newWallet = CryptoWallet(address: '1dalrkje;j34;lkj;lkd;35234');
walletsBox.put(userId, [...existingWallets, newWallet]);
user.wallets.add(newWallet); // throws HiveError: HiveObjects needs to be in the box "wallets"
print (newWallet.box); // --> null

Version

binhphi109 commented 3 years ago

@subzero911 I found a workaround for your case. Already tried and tested.

user.wallets = HiveList(walletsBox).cast();

subzero911 commented 3 years ago

@subzero911 I found a workaround for your case. Already tried and tested.

user.wallets = HiveList(walletsBox).cast();

Thank you for your reply. I found in a docs, that HIve doesn't support collections as a Box generic parameter. image So it's my mistake. The workaround is to create a wrapper WalletList class, that contains this List<CryptoWallet>. So the box type is Box<WalletList>

user.wallets = HiveList(walletsBox).cast();

It's not intended use. HiveList is for relations (list of other objects stored in Hive).