isar / isar/hive

Saving Object with nested List of Objects

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

Description

I have a `Custom-Class` which also has a field with another `Custom-Class`:

part 'hive_vitals_interface.g.dart';

@HiveType(typeId: 1)
class HiveVitals extends HiveObject {
@HiveField(0)
String? id;
@HiveField(1)
DateTime? date;
@HiveField(2)
List otherDiscomfort;
@HiveField(3)
List mentalDiscomfort;

HiveVitals({
this.id,
this.date,
this.otherDiscomfort = const [],
this.mentalDiscomfort = const [],
});
}

And my `HiveDiscomforts-Class`:

part 'hive_discomfort_interface.g.dart';

@HiveType(typeId: 2)
class HiveDiscomfort extends HiveObject {
@HiveField(0)
String? title;
@HiveField(1)
int? intensity;

HiveDiscomfort({
this.title,
this.intensity,
});
}

I am trying to **save** `HiveVitals` like this:

static Future addVitals(HiveVitals hiveVitals) async {
final vitalsBox = getVitalsBox();

await vitalsBox.put(hiveVitals.date!.toIso8601String(), hiveVitals);

}

And **retrieve** it like this:

static List getVitals() {
Box box = getVitalsBox();
List hiveVitals = box.values.toList();
return hiveVitals;
}

**Problem:**

I don't get any errors. In fact when saving my object and checking it in the debugger, everything is saved correctly. However when restarting the app, my `List` fields are always empty again! But the rest of the `HiveVitals`-Fields are still saved correctly!?

What am I missing here? I don't get it... Any help is appreciated! Let me know if you need anything else!

**Version**
- Platform: iOS
- Flutter version: [latest]
- Hive version: [latest]

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with addVitals and getVitals, then inspect hive_vitals_interface.g.dart and hive_discomfort_interface.g.dart, which are the generated files named in the report. Reproduce the save, restart, and retrieval flow and trace how the nested lists are serialized. Done means HiveVitals retains both HiveDiscomfort lists after an app restart, with the existing scalar fields still intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
database, mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.