The app throws a HiveError saying 'box has already been opened' or 'Cannot write, unknown type' when reading or writing local data with Hive.

Register every type adapter exactly once at startup, open each box only once and reuse the reference, and regenerate adapters whenever the model changes.

Step-by-Step Guide

1

Call Hive.registerAdapter() only once at app startup, before runApp()

2

Make sure each model's typeId in @HiveType doesn't collide with another model

3

Check Hive.isBoxOpen('boxName') before attempting to open it again

4

Store the opened Box reference in a service/singleton instead of reopening it

5

Regenerate adapters after model changes: flutter pub run build_runner build --delete-conflicting-outputs

6

Double-check @HiveField index numbers are unique and unchanged for existing data

7

Call Hive.initFlutter() before any box access, ideally in main()

8

Close boxes properly on app disposal to avoid data corruption

Found an issue with this solution?

Related Topics

hive data corruption recoveryflutter build runner conflictshive vs sqflite comparison