8

I'm currently implementing Hive in my FlutterApp. Unfortunately this error pops up all the time:

HiveError: There is already a TypeAdapter for typeId 100.

This is my object:

@HiveType(typeId: 100)
class ShopList{
  @HiveField(0)
  String name;
  @HiveField(1)
  List<ListProduct> products = List();

  ShopList({this.name, this.products});

That's the AUTO-GENERATED adapter:

class ShopListAdapter extends TypeAdapter<ShopList> {
  @override
  final int typeId = 100;

  @override
  ShopList read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int, dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return ShopList(
      name: fields[0] as String,
      products: (fields[1] as List)?.cast<ListProduct>(),
    );
  }

  @override
  void write(BinaryWriter writer, ShopList obj) {
    writer
      ..writeByte(2)
      ..writeByte(0)
      ..write(obj.name)
      ..writeByte(1)
      ..write(obj.products);
  }

  @override
  int get hashCode => typeId.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is ShopListAdapter &&
          runtimeType == other.runtimeType &&
          typeId == other.typeId;
}

I have some other Objects with the typeIds 101 and 102, which throw the same error. The Hive.registerAdapter(ShopListAdapter)); runs in a try-catch-block. So if the adapter were already loaded, the code can just go on, BUT the FutureBuilder-Widget using the value from the Hive loads infinitely long. Do you have any tips?

2
  • 1
    I got the same problem, as this is my first time using hive, waiting some one to give the answer, thanks
    – khalid
    Commented Mar 28, 2021 at 19:10
  • Same problem here! Commented Nov 10, 2021 at 17:05

5 Answers 5

15

I was using an UserAdapter type id=0 so you should check before calling the Hive.registerAdapter as i showed you the below code

enter image description here

 if (!Hive.isAdapterRegistered(0)) {
      Hive.registerAdapter(UserAdapter());
    }
6

you need to check typeId, they should be different and unique from other class models even in same file.

@HiveType(typeId: 0)
class Module1 {}

@HiveType(typeId: 1)
class Module2 {}

Once typeId has been changed then

reinstall the app,
delete .g.dart generated file,
run command ---> flutter packages pub run build_runner build
2
  • It is the same as above though, I run this command to recreate a ".g.dart" file. flutter packages pub run build_runner build --delete-conflicting-outputs
    – Sankame
    Commented Nov 14, 2021 at 11:24
  • yes, --delete-conflicting-outputs, till work as wll Commented Nov 19, 2021 at 9:14
0

If you are sure you have registered the adapter, then that must be cache issue, run flutter clean and MOST IMPORTANTLY Unstill the app and run it afresh. The problem will be so

0

To resolve this issue, you need to ensure that each Hive class has a unique typeId value. The @HiveType(typeId: 0) annotation is used to specify a unique type identifier for a class in Hive. If you have multiple classes, make sure that the typeId values are distinct for each class. For example, you can use @HiveType(typeId: 1) for one class, @HiveType(typeId: 2) for another, and so on.

-1

Friend, I used the command Hive.resetAdapters() , it worked for me.enter image description here

1
  • WARNING: This method is only intended to be used for integration and unit tests and SHOULD not be used in production code.
    – Afinas EM
    Commented Apr 13, 2023 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.