Hive: Weird bug with TypeAdapter

Created on 12 Oct 2019  路  7Comments  路  Source: hivedb/hive

I don't know what is going on. I made a minimal reproducible sample modifying the counter sample: counter.zip

I/flutter (24325): HiveError: There is already a TypeAdapter for typeId 35.

It loads fine, then refreshes (probably because put has occurred) and things get weird. Why is that happening?

bug

Most helpful comment

I think I know the issue!

I think the Hive database got somehow corrupted. I needed to reinstall my app, now it is working fine. Thanks!

All 7 comments

Your sample works fine for me. What device are you testing on? Maybe try to execute flutter clean.

Well, that's certainly weird. I tried flutter clean without success.
Currently Android Emulator API 29. Just make sure to wait a few seconds (like 3~5 seconds) after opening it.

Hey, could you reproduce it? I'm locked because of it.

I think I know the problem. When you do a hot reload, your _openBox() is called a second time and fails because the TypeAdapter has already been registered.

This should work:
```dart
var initialized = false;
Future _openBox() async {
if (!initialized) {
initialized = true;
if (!isBrowser) {
var dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
}
Hive.registerAdapter(ColorAdapter(), 35);
}

return await Hive.openBox('myBox');
}

Nope. When I press run it gives this.
image

When I hot-restart it works. Really weird.

counter.zip

This works fine for me...

I think I know the issue!

I think the Hive database got somehow corrupted. I needed to reinstall my app, now it is working fine. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SergeShkurko picture SergeShkurko  路  4Comments

MyoLinOo picture MyoLinOo  路  3Comments

ProfileID picture ProfileID  路  4Comments

azilvl picture azilvl  路  3Comments

kaboc picture kaboc  路  3Comments