Question
I have a parent abstract class (Message in this case). And I have different child classes extending it. How do I go about annotating the fields?
This is what I have done and it did generate the adapter but they don't include the fields from the parent.
Code sample
abstract class Message {
@HiveField(0)
int timeStamp;
@HiveField(1)
String senderName;
@HiveField(2)
String senderUsername;
@HiveField(3)
bool isSelf;
@HiveField(4)
String documentId;
Message(this.timeStamp, this.senderName, this.senderUsername);
factory Message.fromFireStore(DocumentSnapshot doc) {
final int type = doc.data['type'];
Message message;
switch (type) {
case 0:
message = TextMessage.fromFirestore(doc);
break;
case 1:
message = ImageMessage.fromFirestore(doc);
break;
case 2:
message = VideoMessage.fromFirestore(doc);
break;
case 3:
message = FileMessage.fromFirestore(doc);
}
message.isSelf = SharedObjects.prefs.getString(Constants.sessionUsername) ==
message.senderUsername;
message.documentId = doc.documentID;
return message;
}
factory Message.fromMap(Map map) {
final int type = map['type'];
Message message;
switch (type) {
case 0:
message = TextMessage.fromMap(map);
break;
case 1:
message = ImageMessage.fromMap(map);
break;
case 2:
message = VideoMessage.fromMap(map);
break;
case 3:
message = FileMessage.fromMap(map);
}
message.isSelf = SharedObjects.prefs.getString(Constants.sessionUsername) ==
message.senderUsername;
return message;
}
Map<String, dynamic> toMap();
}
@HiveType()
class TextMessage extends Message {
@HiveField(5)
String text;
TextMessage(this.text, timeStamp, senderName, senderUsername)
: super(timeStamp, senderName, senderUsername);
factory TextMessage.fromFirestore(DocumentSnapshot doc) {
Map data = doc.data;
return TextMessage.fromMap(data);
}
factory TextMessage.fromMap(Map data) {
return TextMessage(data['text'], data['timeStamp'], data['senderName'],
data['senderUsername']);
}
@override
Map<String, dynamic> toMap() {
Map<String, dynamic> map = Map();
map['text'] = text;
map['timeStamp'] = timeStamp;
map['senderName'] = senderName;
map['senderUsername'] = senderUsername;
map['type'] = 0;
return map;
}
@override
String toString() => '{ senderName : $senderName, senderUsername : $senderUsername, isSelf : $isSelf , timeStamp : $timeStamp, type : 3, text: $text }';
}
@HiveType()
class ImageMessage extends Message {
@HiveField(5)
String imageUrl;
@HiveField(6)
String fileName;
ImageMessage(this.imageUrl,this.fileName, timeStamp, senderName, senderUsername)
: super(timeStamp, senderName, senderUsername);
factory ImageMessage.fromFirestore(DocumentSnapshot doc) {
Map data = doc.data;
return ImageMessage.fromMap(data);
}
factory ImageMessage.fromMap(Map data) {
return ImageMessage(data['imageUrl'],data['fileName'], data['timeStamp'], data['senderName'],
data['senderUsername']);
}
@override
Map<String, dynamic> toMap() {
Map<String, dynamic> map = Map();
map['imageUrl'] = imageUrl;
map['fileName']= fileName;
map['timeStamp'] = timeStamp;
map['senderName'] = senderName;
map['senderUsername'] = senderUsername;
map['type'] = 1;
print('map $map');
return map;
}
@override
String toString() => '{ senderName : $senderName, senderUsername : $senderUsername, isSelf : $isSelf , timeStamp : $timeStamp, type : 3, fileName: $fileName, imageUrl : $imageUrl }';
}
@HiveType()
class VideoMessage extends Message {
@HiveField(5)
String videoUrl;
@HiveField(6)
String fileName;
VideoMessage(this.videoUrl,this.fileName, timeStamp, senderName, senderUsername)
: super(timeStamp, senderName, senderUsername);
factory VideoMessage.fromFirestore(DocumentSnapshot doc) {
Map data = doc.data;
return VideoMessage.fromMap(data);
}
factory VideoMessage.fromMap(Map data) {
return VideoMessage(data['videoUrl'],data['fileName'], data['timeStamp'], data['senderName'],
data['senderUsername']);
}
@override
Map<String, dynamic> toMap() {
Map<String, dynamic> map = Map();
map['videoUrl'] = videoUrl;
map['fileName']= fileName;
map['timeStamp'] = timeStamp;
map['senderName'] = senderName;
map['senderUsername'] = senderUsername;
map['type'] = 2;
return map;
}
@override
String toString() => '{ senderName : $senderName, senderUsername : $senderUsername, isSelf : $isSelf , timeStamp : $timeStamp, type : 3, fileName: $fileName, videoUrl : $videoUrl }';
}
@HiveType()
class FileMessage extends Message {
@HiveField(5)
String fileUrl;
@HiveField(6)
String fileName;
FileMessage(this.fileUrl,this.fileName, timeStamp, senderName, senderUsername)
: super(timeStamp, senderName, senderUsername);
factory FileMessage.fromFirestore(DocumentSnapshot doc) {
Map data = doc.data;
return FileMessage.fromMap(data);
}
factory FileMessage.fromMap(Map data) {
return FileMessage(data['fileUrl'],data['fileName'], data['timeStamp'], data['senderName'],
data['senderUsername']);
}
@override
Map<String, dynamic> toMap() {
Map<String, dynamic> map = Map();
map['fileUrl'] = fileUrl;
map['fileName']= fileName;
map['timeStamp'] = timeStamp;
map['senderName'] = senderName;
map['senderUsername'] = senderUsername;
map['type'] = 3;
return map;
}
@override
String toString() => '{ senderName : $senderName, senderUsername : $senderUsername, isSelf : $isSelf , timeStamp : $timeStamp, type : 3, fileName: $fileName, fileUrl : $fileUrl }';
}
Version
The generator does not support inheritance currently. I'll add it to the next version.
When will the next version release? I also need this feature for my app
I have no exact date yet. Hopefully within the next month.
There is not even a full release of Hive required, only the generator needs an update
Any update on this?
Not really, sorry :/ did not have much time recently
T_T
@leisim
I have no exact date yet. Hopefully within the next month.
There is not even a full release of Hive required, only the generator needs an update
Are you sure you want to allow inheritance? As we spoke about before, Hive is similar to protobuf and that explicitly doesn't support inheritance. While I was thinking about it, I could see why: managing the key integers would prove to be problematic. An alternative is to make the generator build subclasses with composition rather than standard inheritance. Though, now I'm only spitballing here, then you'd have to walk up the inheritance chain (and avoid infinite loops) and look for all the HiveFields that need to be placed in their own fields that get tacked onto the end of each subsequent subclass. An alternative could be allowing abstract HiveTypes, either through a parameter on the annotation or even its own class, and using them explicitly for inheritance/composition work with the generator. Maybe even limit this to 5 or so classes deep, regardless of using abstraction as a base for inheritance. Just some thoughts on the matter.
@adityadroid Thanks for your input, I'll need to think about that.
The newest version of Hive and hive_generator support inheritance.
The newest version of Hive and hive_generator support inheritance.
What is the correct way to generate TypeAdapter of extended class?
Is there an example?
Thanks
What is the correct way to generate TypeAdapter of extended class?
Is there an example?
Do you mean something like this:
@HiveType(typeId: 0)
class Pet {
@HiveField(0)
String name;
@HiveField(1)
int age;
}
@HiveType(typeId: 1)
class Dog extends Pet {
@HiveField(2)
bool likesBones;
}
Yes, thanks
What is the correct way to generate TypeAdapter of extended class?
Is there an example?Do you mean something like this:
And if i want generate only extended class ? It's possible? I'm implementing Clean Code Architecture and i would like separate data level from domain level
class Pet {
String name;
int age;
}
@HiveType(typeId: 0)
class PetModel extends Pet {
//How declare name and age like HiveField?
@HiveField(2)
bool likesBones;
}
Thanks
You have two options:
class Pet {
@HiveField(0)
String name;
@HiveField(1)
int age;
}
@HiveType(typeId: 1)
class Dog extends Pet {
@HiveField(2)
bool likesBones;
}
class Pet {
String name;
int age;
}
@HiveType(typeId: 1)
class Dog extends Pet {
@override
@HiveField(0)
String name;
@override
@HiveField(1)
int age;
@HiveField(2)
bool likesBones;
}
Hey I am facing this error....
HiveError: Cannot write, unknown type: _Type.
I have implemented inhertance relationships in my hive objects recently and this popped up...
I am not able to narrow down the issue of "_Type" though....can you help me out?
Most helpful comment
The newest version of Hive and hive_generator support inheritance.