You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Question
I have a class User which is inherited by another class UserA. I have another class Order that uses UserA in different properties. When I put orders in a Hive box, everything looks ok. However, after a restart, UserA objects are always parsed as User objects, and when I try to get orders from my box I get an error in the Order reader. I don't really understand why and didn't find a solution browsing this repo's issues. The generated adapters look good to me, but I may be missing something.
The error I get is type 'User' is not a subtype of type 'UserA' in type cast.
Code sample
@HiveType(typeId:0)
classUserextendsHiveObject {
@HiveField(0)
finalString id;
@HiveField(2)
String firstName;
@HiveField(3)
String lastName;
...
}
@HiveType(typeId:1)
classUserAextendsUser {
@HiveField(4)
String phoneNumber;
...
}
@HiveType(typeId:3)
classOrderextendsChangeNotifierimplementsComparable<Order> {
@HiveField(0)
finalString id;
...
@HiveField(16)
finalUserA by;
...
}
classUserAdapterextendsTypeAdapter<User> {
@overridefinalint typeId =0;
@overrideUserread(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields =<int, dynamic>{
for (int i =0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
returnUser(
id: fields[0] asString,
firstName: fields[2] asString,
lastName: fields[3] asString,
);
}
...
}
classUserAAdapterextendsTypeAdapter<UserA> {
@overridefinalint typeId =1;
@overrideUserAread(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields =<int, dynamic>{
for (int i =0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
returnUserA(
id: fields[0] asdynamic,
firstName: fields[2] asdynamic,
lastName: fields[3] asdynamic,
phoneNumber: fields[4] asdynamic,
);
}
...
}
classOrderAdapterextendsTypeAdapter<Order> {
@overridefinalint typeId =3;
@overrideOrderread(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields =<int, dynamic>{
for (int i =0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
returnOrder(
fields[0] asString,
...
by: fields[16] asUserA,
...
);
}
...
}
// Adapter registration is done in this order:initHive() async {
awaitHive.initFlutter();
Hive
..registerAdapter<User>(UserAdapter())
..registerAdapter<UserA>(UserAAdapter())
..registerAdapter<Order>(OrderAdapter());
}
Version
Platform: iPhone SE 2020 (iOS 15.3)
Flutter version: 2.8.1
Hive version: 2.0.5
The text was updated successfully, but these errors were encountered:
@saibotma it looks like the correct way to deal with inheritance is to register the child classes adapters before registering the inherited class' adapter.
In my case I no longer encounter any issue when registering my adapters like this:
@saibotma it looks like the correct way to deal with inheritance is to register the child classes adapters before registering the inherited class' adapter.
In my case I no longer encounter any issue when registering my adapters like this:
Question
I have a class
User
which is inherited by another classUserA
. I have another classOrder
that usesUserA
in different properties. When I put orders in a Hive box, everything looks ok. However, after a restart,UserA
objects are always parsed asUser
objects, and when I try to get orders from my box I get an error in theOrder
reader. I don't really understand why and didn't find a solution browsing this repo's issues. The generated adapters look good to me, but I may be missing something.The error I get is
type 'User' is not a subtype of type 'UserA' in type cast
.Code sample
Version
The text was updated successfully, but these errors were encountered: