gpt4 book ai didi

flutter - hive 错误 : There is already a TypeAdapter

转载 作者:行者123 更新时间:2023-12-04 17:23:23 34 4
gpt4 key购买 nike

我目前正在实现 Hive在我的 FlutterApp 中。不幸的是,这个错误一直弹出:

HiveError: There is already a TypeAdapter for typeId 100.


这是我的对象:
@HiveType(typeId: 100)
class ShopList{
@HiveField(0)
String name;
@HiveField(1)
List<ListProduct> products = List();

ShopList({this.name, this.products});
那是自动生成的适配器:
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;
}
我还有其他一些带有 typeId 的对象s 101 和 102,它们抛出相同的错误。 Hive.registerAdapter(ShopListAdapter));try-catch-block 中运行.所以如果适配器已经加载,代码可以继续,但是 FutureBuilder -Widget 使用来自 Hive 的值加载无限长。
你有什么建议吗?

最佳答案

您需要检查 typeId,即使在同一文件中,它们也应该与其他类模型不同且唯一。

@HiveType(typeId: 0)
class Module1 {}

@HiveType(typeId: 1)
class Module2 {}
一旦 typeId 被改变,那么
reinstall the app,
delete .g.dart generated file,
run command ---> flutter packages pub run build_runner build

关于flutter - hive 错误 : There is already a TypeAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64944030/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com