hive icon indicating copy to clipboard operation
hive copied to clipboard

HiveObjects needs to be in the box

Open pishguy opened this issue 2 years ago • 1 comments

I have defined two boxes in Hive as follows:

@HiveType(typeId: 1)
class CartTable extends HiveObject {
  @HiveField(0)
  late int id;

  @HiveField(1)
  late String name;

  @HiveField(2)
  late String image;

  @HiveField(3)
  late HiveList<OrdersTable> orders;
}

@HiveType(typeId: 2)
class OrdersTable extends HiveObject {
  @HiveField(0)
  late int id;

  @HiveField(1)
  late String name;

  @HiveField(2)
  late String description;

  @HiveField(3)
  late int price;

  @HiveField(4)
  late String image;

  @HiveField(5,defaultValue: '')
  String? detail;

  @HiveField(6,defaultValue: '')
  String? note;

  @HiveField(7,defaultValue: 1)
  late int count;

  @HiveField(8,defaultValue: '')
  String? promotionCode;
}

Note: Inside the CartTable box, there is another box named OrdersTable which should have a relationship with each other.

And I opened these two boxes in the main.dart file as follows:

await Hive.openBox<CartTable>('cart', encryptionCipher: HiveAesCipher(key));
await Hive.openBox<OrdersTable>('orders', encryptionCipher: HiveAesCipher(key));

Now, when I try to establish this relationship with the following code, it gives me the following error:

HiveObjects needs to be in the box 'orders'.

Here are the implemented codes:

Box<CartTable> _cartTable= Hive.box<CartTable>('cart');
Box<OrdersTable> _orders = Hive.box<OrdersTable>('orders');


CartTable? cart;

cart = CartTable()
  ..id = store.id
  ..name = store.name
  ..image = store.image;

cart.orders = HiveList(_orders);

final order = OrdersTable()
  ..id = menu.id
  ..name = menu.name
  ..description = menu.description!
  ..image = menu.image
  ..price = menu.price
  ..note=''
  ..detail=''
  ..promotionCode=''
  ..count = 1;

cart.orders.add(order);

cartTable.add(cart);

cart.save();

pishguy avatar Aug 20 '23 07:08 pishguy

Did you find any solution?

ghost avatar Oct 24 '23 20:10 ghost