isar / isar/hive

Hive relationships write and read fail

Open
#425 3 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Question**
Hive relationship is not working.

**Code sample**
```
@HiveType(typeId: 1)
class UserHive extends HiveObject {

@HiveField(0)
String id;

@HiveField(1)
String email;

@HiveField(2)
String phone;

@HiveField(3)
String username;

@HiveField(8)
HiveList properties;

UserHive({
this.id,
this.email,
this.phone,
this.username,

this.properties,
});
}

@HiveType(typeId: 2)
class PropertyHive extends HiveObject {

@HiveField(0)
String id;

@HiveField(1)
String name;

@HiveField(2)
String description;

PropertyHive({
this.id,
this.name,
this.description,
});
}

// Trying to access it from a user repository.
final currentUserBox = await Hive.openBox("currentUser");
final propertyHive = await Hive.openBox("userProperties");
currentUserBox.clear();
UserHive currentUser;
var userProperties = [];
if(currentUserBox.isEmpty || currentUserBox.values.isEmpty) {
final currentUser = UserHive()
..username = user.username
..id = user.id
..email = user.email
..phone = user.phone


currentUserBox.add(currentUser);
} else {
currentUser = currentUserBox.values.firstWhere((element) => element.key == 0);
}


currentUser.properties = HiveList(propertyHive);
print("current user is $currentUser");
print("current user key ${currentUser.key}");
for(final property in user.properties) {

userProperties.add(
PropertyHive()
..id = property.id.toString()
..name = property.name
..description = property.description
);
}

currentUser.properties.addAll(userProperties);

currentUser.save();
```

**Version**
- Platform: iOS, Android, Mac, Windows, Linux, Web
- Flutter version: 1.20.2
- Hive version: 1.4.2

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.