Default value within the `HiveField` annotation
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
Hi!
Have an idea how the default values can be set in easy way for `TypeAdapter`.
All we need - add new field to `HiveField` annotation and then use it during class generation.
```dart
class HiveField {
final int index;
final dynamic defaultValue;
const HiveField(this.index, {this.defaultValue});
}
```
Then in our class we can do like this:
```dart
@HiveType()
class CityHive {
@HiveField(0, defaultValue: -1)
int id;
@HiveField(1, defaultValue: "Some name")
String name;
@HiveField(2)
double lat;
@HiveField(3)
double lng;
}
```
Generated class will looks like this:
```dart
class CityHiveAdapter extends TypeAdapter {
@override
CityHive read(BinaryReader reader) {
var numOfFields = reader.readByte();
var fields = {
for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return CityHive()
..id = fields[0] as int ?? -1
..name = fields[1] as String ?? "Some name"
..lat = fields[2] as double
..lng = fields[3] as double
}
...
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start at the HiveField annotation and the class-generation entry point, then trace how annotated fields become the generated TypeAdapter read method. Verify that defaultValue is carried through for fields that specify it and that the generated adapter output matches the requested examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- databases, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100