Closed vizakenjack closed 2 months ago
Good idea! The documentation for the code generation is surprisingly sparse. I would mention other methods of setting the default value, like with a constructor parameter default.
I would mention other methods of setting the default value, like with a constructor parameter default.
In the previous version of Hive that was not enough, so I had to also specify defaultValue in HiveField()
If that's the case we should probably fix that
@vizakenjack
I just released hive_ce_generator: 1.5.0
which supports constructor parameter defaults. Please see the code here for an example and update the documentation in this PR to include those methods of specifying default values.
@vizakenjack
I just released
hive_ce_generator: 1.5.0
which supports constructor parameter defaults. Please see the code here for an example and update the documentation in this PR to include those methods of specifying default values.
Wow, that was fast! Thanks. So, if I understood correctly, if I add a new field to the database with @HiveField(1, defaultValue: '6 * 7')
and this.b = '42'
in the constructor, there will be '42' for new objects and '6 * 7' for old objects?
If I add d = d ?? DateTime.now();
, there won't be any migration for null values and there could be an error, like in the previous version?
if I add a new field to the database with @HiveField(1, defaultValue: '6 7') and this.b = '42' in the constructor, there will be '42' for new objects and '6 7' for old objects?
That is correct
If I add d = d ?? DateTime.now();, there won't be any migration for null values and there could be an error, like in the previous version?
That is not correct. The default value is DateTime.now()
in all cases, and that should have been the case before generator version 1.5.0 as well. As we can see in the generated code:
@override
ConstructorDefaults read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return ConstructorDefaults(
a: fields[0] == null ? 42 : (fields[0] as num).toInt(),
b: fields[1] == null ? '6 * 7' : fields[1] as String,
c: fields[2] == null ? true : fields[2] as bool,
d: fields[3] as DateTime?,
);
}
If fields[3]
is null, the default value of DateTime.now()
will be used
@Rexios80 thanks for clarifying this. I have updated the docs
All modified and coverable lines are covered by tests :white_check_mark:
Please upload report for BASE (
main@139cdae
). Learn more about missing BASE report.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
Released in version 2.5.0+2
Thank you for the contribution!
A friend of mine said that he tried to use Hive but had an issue with migrations.
He was not able to figure out that there is a defaultValue parameter in the @HiveField() annotation. I think it's worth mentioning this issue in the documentation.