【问题标题】:Hive Error - The type 'TypeAdapter' is declared with 0 type parameters, but 1 type arguments were givenHive 错误 - 使用 0 个类型参数声明类型“TypeAdapter”,但给出了 1 个类型参数
【发布时间】:2021-10-14 14:03:49
【问题描述】:

我正在使用 Hive 模型,即使在我运行时,我也已将 @HiveType(typeId: x) 正确分配给每个类

 flutter packages pub run build_runner build

我在为 typeId 2 到 10 的模型生成的文件中遇到错误,下面是错误,这些从 typeId 2 到 10 的模型是后来添加的,所以我必须删除以前生成的文件并重新运行命令,然后 Hive 生成TypeAdapters for Type 0 和 Type 1 即 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

【问题讨论】:

    标签: android ios flutter dart hive


    【解决方案1】:

    感谢 Matheus Baumgarten,https://github.com/hivedb/hive/issues/811

    I was having a model called Type;
    

    当 Hive 尝试生成适配器时,它使用您的模型名称 + 适配器后缀;喜欢

    class TypeAdapter extends TypeAdapter<Type> {}
    

    Generated 适配器然后在范围内,dart 无法确定其他类试图扩展的 TypeAdapter 来自 Hive。

    所以通过添加adapterName解决了这个问题,

      /// In your models file
      @HiveType(typeId: 5, adapterName: "MyTypeAdapter")
      class Type {
         /* ... */
      }
    

    哪个生成的文件将具有名为 MyTypeAdapter 的 Type 模型,它将实现我们的 Type 模型

        class MyTypeAdapter extends TypeAdapter<Type> {
          @override
          final int typeId = 5;
    
          @override
          Type read(BinaryReader reader) {
            final numOfFields = reader.readByte();
            final fields = <int, dynamic>{
              for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
            };
            return Type(
              typeID: fields[0] as int,
              name: fields[1] as String,
            );
          }
        }
    

    【讨论】:

      猜你喜欢
      • 2022-08-12
      • 2021-07-17
      • 2020-04-12
      • 1970-01-01
      • 2018-09-28
      • 2021-01-08
      • 1970-01-01
      • 2020-12-29
      相关资源
      最近更新 更多