我正在使用Hive模型,我已经将@HiveType(typeId: x)
正确分配给每个类,即使当我运行
flutter packages pub run build_runner build
我在为typeId为2到10的模型生成的文件中出错。下面是错误。从typeId 2到10的这些模型是后来添加的,所以我必须删除以前生成的文件并删除命令,Hive为Type 0和Type 1生成的TypeAdapters即Module和UserData没有问题。
The type 'TypeAdapter' is declared with 0 type parameters, but 1 type arguments were given.
Try adjusting the number of type arguments to match the number of type
parameters.dart(wrong_number_of_type_arguments)
This issue is for only models with typeId:2 to typeId:10
下面是我的@HiveType
和@HiveField
模型。
文件1型号:
@HiveType(typeId: 0)
class UserData {
@HiveField(0)
String token;
int userID;
@HiveField(1)
String firstName;
UserData({
this.userID,
this.firstName,
});
}
@HiveType(typeId: 1)
class Module {
@HiveField(0)
int moduleID;
@HiveField(1)
String moduleName;
Module({
this.moduleID,
this.moduleName,
});
}
文件2型号:
@HiveType(typeId: 2)
class ExamData {
@HiveField(0)
int moduleID;
@HiveField(1)
int broadcastID;
ExamData(
{this.moduleID,
this.broadcastID,});
}
@HiveType(typeId: 3)
class Section {
@HiveField(0)
int sectionID;
@HiveField(1)
String title;
Section(
{this.sectionID,
this.title
});
}
@HiveType(typeId: 4)
class Question {
@HiveField(0)
int questionID;
@HiveField(1)
String title;
Question(
{this.questionID,
this.title});
}
@HiveType(typeId: 5)
class Type {
@HiveField(0)
int typeId;
@HiveField(1)
String name;
Type({this.typeId, this.name});
}
@HiveType(typeId: 6)
class Answer {
@HiveField(0)
int answerID;
@HiveField(1)
String title;
Answer({this.answerID, this.title});
}
@HiveType(typeId: 7)
class FileInfo {
@HiveField(0)
int fileID;
@HiveField(1)
String type;
FileInfo(
{this.fileID,
this.type});
}
@HiveType(typeId: 8)
class Meta {
@HiveField(0)
String tempFieldForHiveTest;
@HiveField(1)
List<Tag> tags;
Meta({this.tags, this.tempFieldForHiveTest: ""});
}
@HiveType(typeId: 9)
class Tag {
@HiveField(0)
int tagID;
@HiveField(1)
int moduleID;
Tag(
{this.tagID,
this.moduleID,});
}
@HiveType(typeId: 10)
class SectionStatus {
@HiveField(0)
Section section;
@HiveField(1)
bool isAttempted;
SectionStatus({this.section, this.isAttempted});
}
下面是我的开发依赖:
dev_dependencies:
hive_generator: ^1.1.1
build_runner: ^2.0.6
flutter_launcher_icons: ^0.9.2
1条答案
按热度按时间ohfgkhjo1#
我在Hive Repo的github上得到了我的问题的答案,感谢Matheus Baumgarten,https://github.com/hivedb/hive/issues/811
当Hive尝试生成一个适配器时,它使用你的模型名称+ Adapter后缀;像
然后,生成的适配器在范围内,dart无法确定其他类试图扩展的TypeAdapter来自Hive。
所以通过添加adapterName解决了这个问题,
生成的文件将具有名为MyTypeAdapter的类型模型,该类型模型将实现我们的类型模型