'HiveError: There is already a TypeAdapter
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?
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mrudul Addipalli |
