isar / hive

Lightweight and blazing fast key-value database written in pure Dart.
Apache License 2.0
4.1k stars 407 forks source link

HiveError: Cannot read, unknown typeId: 34. Did you forget to register an adapter? when Removing an Adapter #1207

Open minoesteban opened 1 year ago

minoesteban commented 1 year ago

Steps to Reproduce

  1. Create custom Adapter MainClass, create custom Adapter Property which is a property of A. Register
  2. Write data
  3. Remove adapter B
  4. Read old data.

Reading fails with message HiveError: Cannot read, unknown typeId: 34. Did you forget to register an adapter?

Based on the docs, reading old data after removing a property should be safe, but it throws

Found another possible solution in this comment

But we added Hive.ignoreTypeId(N) and now the error has changed, but it is still failing

The new error is RangeError: Not enough bytes available.

Code sample

// Initialization 
Future<void> initializeHive() async {
  await Hive.initFlutter();

  Hive.registerAdapter(MainClassAdapter());
  Hive.registerAdapter(PropertyAdapter());
  ...
}

//  Classes definition
@HiveType(typeId: 1)
class MainClass {
  MainClass({
    required this.property,
  });
...
  @HiveField(N)
  final Property property;

  @HiveType(typeId: 2)
class Property {
  Property({required this.name});

  @HiveField(0)
  final String name;
}

// After writing some data with this structure, 
// we removed Property from the registerAdapter calls, and form the MainClass definition

But we started getting HiveError: unknown typeId when reading old data

// Then, we tried with

// Initialization 
Future<void> initializeHive() async {
  await Hive.initFlutter();

  Hive.registerAdapter(MainClassAdapter());
  Hive.ignoreTypeId(2);
  ...
}

// But now we get RangeError: Not enough bytes available.

Version

MohamedAboElM3aTy commented 1 year ago

I think you should add adapterName like that issue

minoesteban commented 1 year ago

Could you elaborate on the need for that extra parameter for a solution here? It seems that if not set a default one is created, that is already unique to each adapter

umutbariscoskun commented 1 year ago

Same here, i faced this after upgrading flutter 3.10.2

AliEasy commented 1 year ago

Any update on this?!

BoBoSama commented 1 year ago

deleteFromDisk() and open again

waslost0 commented 1 year ago

Try add ignoreTypeId before registerAdapter

Hive.ignoreTypeId(2);
Hive.registerAdapter(MainClassAdapter());
LaxmikanthMadhyastha commented 8 months ago

I'm also facing this issue with Flutter version 3.16.9

Kiruel commented 1 month ago

@minoesteban Did you find a solution finally ?