Allow non-const default values
Is your feature request related to a problem? Please describe.
We can now set default values. I think that's great, especially for when we introduce new non-nullable fields, so they're always guaranteed to have a value. The problem is that the default value needs to be a const expression. This means it can't be an instance of a subclass of HiveObjectMixin (which all of our non-primitive types are), because that doesn't have a const constructor.
Here's a simple example, say you have a new non-primitive field:
class NewComplexField with HiveObjectMixin {}
I would want to do:
class MyObject with HiveObjectMixin {
@HiveField(14, defaultValue: NewComplexField())
final NewComplexField complexField;
}
except that's not possible.
Describe the solution you'd like I think it be fixed by having a function generate default values. Something like
class MyObject with HiveObjectMixin {
@HiveField(14, defaultValue: () => NewComplexField())
final NewComplexField complexField;
}
Version
- Platform: iOS, Android, Mac, Windows, Linux, Web
- Flutter version: 2.0.6
- Hive version: 2.0.4
Unfortunately codegeneration for default values is limited by constant values. What you can do is you can write your own adapter to supply non-constant value.
Unfortunately codegeneration for default values is limited by constant values. What you can do is you can write your own adapter to supply non-constant value.
Can you give an example of your suggestion? I am trying to do something like this too
class PatientData extends HiveObject {
@HiveField(0,defaultValue:AccessToken())
AccessToken? accessToken;
any further information on how I can support the non-constant value?
any further information on how I can support the non-constant value?
Not tested but you might try:
- Remove defaultValue use hive_generator to generate adapter for you.
- Copy contents of .g.dart file into separate file
- Modify read method to support custom substitute for null values