isar / isar/hive

type 'BasedPersonVO' is not a subtype of type 'PersonVO' in type cast Hive Flutter

Open
#682 0 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Question**
type 'BasedPersonVO' is not a subtype of type 'PersonVO' in type cast Hive Flutter

I am new to hiva and encounter the problem while using hive. Super class is not a subtype of extended class. Here is my two classes and error occur when i retrieve PersonVO from hive.

I already re-register adapter and check generated file.And I don't know why the problem still persist.

Here is the Error

```
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. `(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)
```
Here is my based class BasePersonVO class

```
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 json) => _$BasedPersonVOFromJson(json);

Map toJson() => _$BasedPersonVOToJson(this);
}

```
BasedPersonVO generated class

```
part of 'based_person_vo.dart';

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

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

@override
BasedPersonVO read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = {
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 json) {
return BasedPersonVO(
json['name'] as String,
json['profile_path'] as String,
);
}

Map _$BasedPersonVOToJson(BasedPersonVO instance) =>
{
'name': instance.name,
'profile_path': instance.profilePath,
};
```
PersonVO class
```
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 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 json) =>
_$PersonVOFromJson(json);
Map toJson() => _$PersonVOToJson(this);
}
```

PersonVO generated class

```
part of 'person_vo.dart';

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

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

@override
PersonVO read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return PersonVO(
fields[2] as bool,
fields[3] as int,
fields[4] as int,
(fields[5] as List)?.cast(),
fields[0] as String,
fields[1] as String,
);
}

@override
void write(BinaryWriter writer, 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 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))
?.toList(),
json['name'] as String,
json['profile_path'] as String,
);
}

Map _$PersonVOToJson(PersonVO instance) => {
'name': instance.name,
'profile_path': instance.profilePath,
'adult': instance.adult,
'gender': instance.gender,
'id': instance.id,
'known_for': instance.knownFor,
};
```

PersonDao class **Error occur here when i called getAllperson()** method.

```

class PersonDao{


static final PersonDao _singleton = PersonDao._internal();

factory PersonDao(){
return _singleton;
}


PersonDao._internal();

void saveAllPerson(List personList) async {
Map personMap = Map.fromIterable(personList,key: (person) => person.id, value: (person) => person);
await getPersonBox().putAll(personMap);
}

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

Box getPersonBox(){
return Hive.box(BOX_NAME_PERSON_VO);
}

}
```

Thanks for your time.

**Version**
- Platform: Android
- Flutter version: 2.0
- Hive version: 2.0.0

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.