I am trying to understand the rational behind using writeByte(3)
in the write
method in Hive TypeAdapter.
Please consider the class:
@HiveType()
class Person{
@HiveField(0)
String name;
@HiveField(1)
int age;
}
In the TypeAdapter
below It is easy to understand the read
method, since it is just reads sequentially each field.
However, I'm trying to figure out why the same mechanism does not apply to the write
, instead of using ..writeByte(...)
just before each field. And, what is the meaning of the first ..writeByte(2)
?
class PersonAdapter extends TypeAdapter<Person> {
@override
Person read(BinaryReader reader) {
var numOfFields = reader.readByte();
var fields = <int, dynamic>{
for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Trips()
..name = fields[0] as String
..age = fields[1] as int;
}
@override
void write(BinaryWriter writer, Person obj) {
writer
..writeByte(2) // Why this here? (sometimes I see writeByte(3) !! )
..writeByte(0)
..write(obj.name)
..writeByte(1)
..write(obj.age);
}
}
Thanks for any clarification.