hive icon indicating copy to clipboard operation
hive copied to clipboard

Adding new field to Hive Object results in error from Adapter

Open santiLabhouse opened this issue 4 years ago • 12 comments

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

santiLabhouse avatar Sep 10 '21 08:09 santiLabhouse

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;

   ....
}

EdwynZN avatar Sep 10 '21 18:09 EdwynZN

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

vstacked avatar Jan 08 '22 17:01 vstacked

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 avatar Mar 03 '23 03:03 NengStyleCnb

@NengStyleCnb did you found any solution ? It would be helpful if you can provide details.

sharath-inunity avatar Oct 30 '23 16:10 sharath-inunity

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.

image

NengStyleCnb avatar Oct 31 '23 04:10 NengStyleCnb

are you able to add new value to new field to old data ?

sharath-inunity avatar Oct 31 '23 04:10 sharath-inunity

@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 avatar Oct 31 '23 04:10 NengStyleCnb

@NengStyleCnb Thanks. Solved my issue.

sharath-inunity avatar Oct 31 '23 14:10 sharath-inunity

The problem was beautifully solved. Thank you so mush.

flexrun avatar Dec 22 '23 08:12 flexrun

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 ♥♥♥♥♥♥

reza-najafi1998 avatar Jun 13 '24 11:06 reza-najafi1998

Thankyou so muchh

ShashwatDhingra avatar Jun 27 '24 08:06 ShashwatDhingra

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

Narz797 avatar Jul 18 '24 14:07 Narz797