flutter - HiveError:已经有一个 TypeAdapter
问题描述
我目前正在Hive
我的 FlutterApp 中实施。不幸的是,这个错误一直弹出:
HiveError: typeId 100 已经有一个 TypeAdapter。
这是我的对象:
@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
推荐阅读
- python - 在 Jupyter Notebooks 中从 Python 编辑 SQL 查询
- haskell - 为什么 Haskell 不能推断出这种类型
- apache-kafka - 低延迟分布式日志
- java - 图表着色所有可能的解决方案,特殊情况
- javascript - 如何防止 `single-value` div 中的文本溢出到 React-Select 中的 `indicators` div
- vuetify.js - 如何根据模型设置警报类型?
- ruby-on-rails - :selected 与 rails 表单中的 collection_select 一起使用时不起作用
- jquery - 如何查看从 web 方法返回的 json 字符串?
- javascript - (中间值).then 不是函数
- c - 如何在函数中动态分配数组?