ios 配置单元错误-使用0个类型参数声明了类型“TypeAdapter”,但提供了1个类型参数

acruukt9  于 12个月前  发布在  iOS
关注(0)|答案(1)|浏览(117)

我正在使用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
ohfgkhjo

ohfgkhjo1#

我在Hive Repo的github上得到了我的问题的答案,感谢Matheus Baumgarten,https://github.com/hivedb/hive/issues/811

I was having a model called Type;

当Hive尝试生成一个适配器时,它使用你的模型名称+ Adapter后缀;像

class TypeAdapter extends TypeAdapter<Type> {}

然后,生成的适配器在范围内,dart无法确定其他类试图扩展的TypeAdapter来自Hive。
所以通过添加adapterName解决了这个问题,

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

生成的文件将具有名为MyTypeAdapter的类型模型,该类型模型将实现我们的类型模型

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,
        );
      }
    }

相关问题