the getter isn't defined for the type 'hiveobjectmixin'
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
After creating model to make a relationship between 2 models i can't get field data from nested relationship
I have a User model which that contain other model as Transactions which that's a `HiveList`, now when i try to get a field such as `cost` from Transaction with below code:
```dart
final Box _user = Hive.box('user');
print('${_user.values.first.transactions.first.cost}');
```
i get this error:
> error: The getter 'cost' isn't defined for the type 'HiveObjectMixin'.
> (undefined_getter at [xxxx]
> lib/src/screens/dashboard/tabs/screen_transactions.dart:85)
Models:
```dart
@HiveType(typeId: 1)
class User extends HiveObject {
@HiveField(0)
late String nameFamily;
@HiveField(1)
late String apiToken;
@HiveField(2)
late String mobileNumber;
@HiveField(3)
late HiveList transactions;
}
@HiveType (typeId: 4)
class Transactions extends HiveObject {
@HiveField(0)
late int cost;
@HiveField(1)
late int type;
@HiveField(2)
late String dateTime;
}
```
Adapters:
```dart
class UserAdapter extends TypeAdapter {
@override
final int typeId = 1;
@override
User read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return User()
..nameFamily = fields[0] as String
..apiToken = fields[1] as String
..mobileNumber = fields[2] as String
..address = (fields[3] as HiveList).castHiveList()
..wallet = (fields[4] as HiveList).castHiveList()
..transactions = (fields[5] as HiveList).castHiveList();
}
@override
void write(BinaryWriter writer, User obj) {
writer
..writeByte(6)
..writeByte(0)
..write(obj.nameFamily)
..writeByte(1)
..write(obj.apiToken)
..writeByte(2)
..write(obj.mobileNumber)
..writeByte(3)
..write(obj.transactions);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is UserAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
class WalletAdapter extends TypeAdapter {
@override
final int typeId = 3;
@override
Wallet read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Wallet()..total = fields[0] as int;
}
@override
void write(BinaryWriter writer, Wallet obj) {
writer
..writeByte(1)
..writeByte(0)
..write(obj.total);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is WalletAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start at lib/src/screens/dashboard/tabs/screen_transactions.dart:85 and compare the User.transactions declaration with the Transactions model and generated adapter code shown in the issue. Reproduce the nested access and inspect how HiveList elements are typed; done means the Transactions cost field can be accessed without the HiveObjectMixin getter error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100