hive
hive copied to clipboard
Adding new field to Hive Object results in error from Adapter
Question I've added 2 new fields to my existing Hive Object. I followed the rules, didn't change the HiveType and also the new fields have new HiveField numbers. I generated the new adapter using the command
flutter packages pub run build_runner build
The problem is that when I run my app and openBox is executed, the following exception is thrown:
Unhandled Exception: type 'Null' is not a subtype of type 'String' in type cast
I can see through console that the new fields are the problem, because the previous stored data in that box didn't have that fields. I read the documentation and there says that this is not supposed to happen.
Code sample
await Hive.initFlutter();
Hive.registerAdapter(ProductsAdapter());
await Hive.openBox<Product>("favorite_products"); // this throws the exception
Version
- Platform: iOS, Android
- Flutter version: 2.2.1
- Hive version: 2.0.4
Perhaps is a problem of incompatibility because your new fields are of type String (not String?) so when reading them from the box they are null, maybe use the defaultValue of the generator to avoid this case.
@HiveType(typeId: 1)
Product{
@HiveField(5, defaultValue: '')
String newString;
....
}
You need to delete old box first on disk before called new adapter \ openBox, with await Hive.deleteBoxFromDisk(YOUR_BOX_KEY);
Try add :
await Hive.initFlutter();
Hive.registerAdapter(ProductsAdapter());
await Hive.deleteBoxFromDisk('favorite_products'); // THIS LINE, then delete it
await Hive.openBox<Product>("favorite_products");
Or you can just put that line on dummy button, pressed, and restart
I have meet this issue also but your suggestion @vstacked try to delete box first, how about old data that store on previous version? So, it mean all data is empty box, it is not make sense. But @EdwynZN your suggestion look like better but this way is working well? Thanks all.
Hive version --> hive: ^2.2.3 Flutter version --> hive_flutter: ^1.1.0
@NengStyleCnb did you found any solution ? It would be helpful if you can provide details.
Hello @sharath-inunity Yeah, currently I have used it the style @EdwynZN have mentioned and see it working fine. We can keep old data and add new field with default value.
are you able to add new value to new field to old data ?
@sharath-inunity I'm not clear your question but you should set value to new field with attribution "defaultValue". if you want to update old data (old field) you get box and update box with new data.
@NengStyleCnb Thanks. Solved my issue.
The problem was beautifully solved. Thank you so mush.
Perhaps is a problem of incompatibility because your new fields are of type String (not String?) so when reading them from the box they are null, maybe use the defaultValue of the generator to avoid this case.
@HiveType(typeId: 1) Product{ @HiveField(5, defaultValue: '') String newString; .... }
Thank you ♥♥♥♥♥♥
Thankyou so muchh
Hello, I'm just new to flutter and is having the same issue, tried doin both solutions of @vstacked and @EdwynZN but no luck. Code runs but when I try to add new data, the app crashes
https://github.com/user-attachments/assets/c0941db6-26c8-468d-ba85-1ef5cedb0200
pls help