Question
Please explain the problem you are running into.
I am using flutter and storing custom object data, I want to check whether a particular object already exist in the Hive DB or not based on ID, If available update else insert as new object.
Code sample
```My custom object is
@HiveType(typeId: 0)
class ProductDBModel extends HiveObject{
@HiveField(0)
String id;
@HiveField(1)
String productName;
@HiveField(2)
String image;
@HiveField(3)
String price;
@HiveField(4)
String description;
ProductDBModel(this.id, this.productName, this.image, this.price, this.description);
}
```
Version
Add an int key field to your model and then try this out:
/// If class exists, update it, else insert it and assign the auto incremented key to it
Future<void> upsert(YourModel model) async {
final modelExists = yourBox.containsKey(model.key);
if (!modelExists) {
final key = await yourBox.add(model);
model.key = key;
}
model.save();
}
Thank you for the quick reply I had modified code as below, what i am trying to do is I am passing object to CheckUpdate method, in hive if object is not available I am able to add new object but if object already exist I want to increment quantity value only of that particular object only.
@HiveType(typeId: 0)
class ProductDBModel extends HiveObject{
@HiveField(0)
int id;
@HiveField(1)
String productName;
@HiveField(2)
String image;
@HiveField(3)
int price;
@HiveField(4)
String description;
@HiveField(5)
int quantity;
ProductDBModel(this.id, this.productName, this.image, this.price, this.description, this.quantity);
}
CheckUpdate(widget.product);
Future<void> CheckUpdate(ProductDBModel product) async {
Box prodBox = await Hive.openBox<ProductDBModel>(ProductTable);
final modelExists = prodBox.containsKey(product.id);
if (!modelExists) {
product.quantity = 0;
await prodBox.add(product);
}
else{
product.quantity = 1;
}
product.save();
}
You are setting product.quantity to 1, not incrementing it.
If you want to increment the quantity, you could just do any of the following:
product.quantity = product.quantity + 1;
or ...
product.quantity += 1;
or...
product.quantity++;
This is not the optimal way, but should get you going!
Edit: be sure to link the key provided by box.add() with your model's id! Otherwise you might get wrong returns with box.containsKey()
Hi I have changed the code as below
Future
Box prodBox = await Hive.openBox
final modelExists = prodBox.containsKey(product.id);
if (!modelExists) {
product.quantity = 1;
await prodBox.add(product);
}
else{
ProductDBModel pd = prodBox.get(product.id);
pd.quantity = pd.quantity+1;
await pd.save();
}
_showToast(context, 'item added to cart');
}
but I am getting error like Unhandled Exception: HiveError: The same instance of an HiveObject cannot be stored with two different keys ("2" and "3").
The product.id is not the same as the key of your object!
For prodBox.containsKey() to work tou need to use the key from your hive object!
Copy the code below and try it directly.
Future<void> checkUpdate(ProductDBModel product) async {
Box prodBox = await Hive.openBox(ProductTable);
final modelExists = prodBox.containsKey(product.key);
if (!modelExists) {
product.quantity = 1;
await prodBox.add(product);
} else {
product.quantity += 1;
product.save();
}
}
tq its working...