在类型转换 Hive Flutter 中,'BasedPersonVO' 类型不是'PersonVO' 类型的子类型

问题描述

我是 Flutter 的新手。我在使用 hive 时遇到了这个问题。 超类不是扩展类的子类型。 这是我的两个类,当我从 hive 中检索 PersonVO 时发生错误

我已经重新注册了适配器并检查了生成文件。但我不知道为什么问题仍然存在。

    The following _CastError was thrown building Builder:
type 'BasedPersonVO' is not a subtype of type 'PersonVO' in type cast

The relevant error-causing widget was: 
  MaterialApp file:///home/thurain/FlutterProjects/movie_app/lib/main.dart:46:12
When the exception was thrown,this was the stack: 
#0      Keystore.getValues.<anonymous closure> (package:hive/src/Box/keystore.dart:124:45)
#1      MappedIterator.moveNext (dart:_internal/iterable.dart:389:20)
#2      new _GrowableList._ofOther (dart:core-patch/growable_array.dart:198:26)
#3      new _GrowableList.of (dart:core-patch/growable_array.dart:152:26)
#4      new List.of (dart:core-patch/array_patch.dart:50:28)

这是我的基础类 BasePersonVO 类

part 'based_person_vo.g.dart';

@HiveType(typeId: HIVE_TYPE_ID_BASED_PERSON_VO,adapterName: 'BasedPersonVOAdapter')
@JsonSerializable()
class BasedPersonVO{
  @HiveField(0)
  @JsonKey(name: 'name')
  String name;

  @HiveField(1)
  @JsonKey(name: 'profile_path')
  String profilePath;


  BasedPersonVO(this.name,this.profilePath);

  factory BasedPersonVO.fromJson(Map<String,dynamic> json) => _$BasedPersonVOFromJson(json);

  Map<String,dynamic> toJson() => _$BasedPersonVOToJson(this);
}

BasedPersonVO 生成

part of 'based_person_vo.dart';

// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************

class BasedPersonVOAdapter extends TypeAdapter<BasedPersonVO> {
  @override
  final int typeId = 1;

  @override
  BasedPersonVO read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int,dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),};
    return BasedPersonVO(
      fields[0] as String,fields[1] as String,);
  }

  @override
  void write(BinaryWriter writer,BasedPersonVO obj) {
    writer
      ..writeByte(2)
      ..writeByte(0)
      ..write(obj.name)
      ..writeByte(1)
      ..write(obj.profilePath);
  }

  @override
  int get hashCode => typeId.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this,other) ||
      other is BasedPersonVOAdapter &&
          runtimeType == other.runtimeType &&
          typeId == other.typeId;
}

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

BasedPersonVO _$BasedPersonVOFromJson(Map<String,dynamic> json) {
  return BasedPersonVO(
    json['name'] as String,json['profile_path'] as String,);
}

Map<String,dynamic> _$BasedPersonVOToJson(BasedPersonVO instance) =>
    <String,dynamic>{
      'name': instance.name,'profile_path': instance.profilePath,};

PersonVO 类

part 'person_vo.g.dart';

@HiveType(typeId: HIVE_TYPE_ID_PERSON_VO,adapterName: 'PersonVOAdapter')
@JsonSerializable()
class PersonVO extends BasedPersonVO{

  @HiveField(2)
  @JsonKey(name: 'adult')
  bool adult;

  @HiveField(3)
  @JsonKey(name: 'gender')
  int gender;

  @HiveField(4)
  @JsonKey(name: 'id')
  int id;

  @HiveField(5)
  @JsonKey(name: 'kNown_for')
  List<KNownForVO> kNownFor;

  // @JsonKey(name: 'name')
  // String name;
  //
  // @JsonKey(name: 'profile_path')
  // String profilePath;

  PersonVO(this.adult,this.gender,this.id,this.kNownFor,String name,String profilePath) : super(name,profilePath);

  factory PersonVO.fromJson(Map<String,dynamic> json) =>
      _$PersonVOFromJson(json);
  Map<String,dynamic> toJson() => _$PersonVOToJson(this);
}

PersonVO 生成

part of 'person_vo.dart';

// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************

class PersonVOAdapter extends TypeAdapter<PersonVO> {
  @override
  final int typeId = 8;

  @override
  PersonVO read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int,};
    return PersonVO(
      fields[2] as bool,fields[3] as int,fields[4] as int,(fields[5] as List)?.cast<KNownForVO>(),fields[0] as String,PersonVO obj) {
    writer
      ..writeByte(6)
      ..writeByte(2)
      ..write(obj.adult)
      ..writeByte(3)
      ..write(obj.gender)
      ..writeByte(4)
      ..write(obj.id)
      ..writeByte(5)
      ..write(obj.kNownFor)
      ..writeByte(0)
      ..write(obj.name)
      ..writeByte(1)
      ..write(obj.profilePath);
  }

  @override
  int get hashCode => typeId.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this,other) ||
      other is PersonVOAdapter &&
          runtimeType == other.runtimeType &&
          typeId == other.typeId;
}

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

PersonVO _$PersonVOFromJson(Map<String,dynamic> json) {
  return PersonVO(
    json['adult'] as bool,json['gender'] as int,json['id'] as int,(json['kNown_for'] as List)
        ?.map((e) =>
            e == null ? null : KNownForVO.fromJson(e as Map<String,dynamic>))
        ?.toList(),json['name'] as String,dynamic> _$PersonVOToJson(PersonVO instance) => <String,'adult': instance.adult,'gender': instance.gender,'id': instance.id,'kNown_for': instance.kNownFor,};

PersonDao 类 当我调用 getAllperson() 方法时出现错误

class PersonDao{
  
  
  static final PersonDao _singleton = PersonDao._internal();
  
  factory PersonDao(){
    return _singleton;
  }
  
  
  PersonDao._internal();
  
  void saveAllPerson(List<PersonVO> personList) async {
    Map<int,PersonVO> personMap = Map.fromIterable(personList,key: (person) => person.id,value: (person) => person);
     await getPersonBox().putAll(personMap);
  }

  List<PersonVO> getAllPerson(){
    return getPersonBox().values.toList();
  }

  Box<PersonVO> getPersonBox(){
    return Hive.Box<PersonVO>(Box_NAME_PERSON_VO);
  }
  
}

感谢您的时间。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)