【发布时间】:2022-01-17 22:56:24
【问题描述】:
假设我有以下 3 个类:
class Human{
String name;
Human({this.name});
}
class Student extends Human{
int id;
Student(this.id): super(name: name);
}
class Proffessor extends Human{
int age;
Proffessor(this.age): super(name: name);
}
class Abc {
List<Human> humans;
Abc(this.humans);
//Just Imagine my humans List consists of Proffessors AND Students (both Subtypes of human)
}
我正在使用以下依赖项 hive_generator: ^1.1.1 hive_flutter:^1.1.0 蜂巢:^2.0.4 dev_dependencies: build_runner:^2.1.4 基本上,我有一个由教授和学生组成的列表,但是当这个列表被保存并稍后从 Hive 中检索时,该列表中的所有对象都被强制转换为人类
Abc read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++)
reader.readByte(): reader.read(),
};
return Abc(
fields[0] as int,
fields[1] as String?,
fields[2] as String?,
(fields[3] as List?)?.cast<Human>(), // I need the Elements in this List as Student / Proffessor not Human
fields[4] as int,
);}
//基本上我正在丢失信息,因为适配器将学生和教授转换为人类
有解决办法吗?如果不是,那么 hive 的下一个最佳替代品是什么?
【问题讨论】:
标签: flutter flutter-hive