isar / isar/hive

Error while retrieve list of models

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

Description

**Steps to Reproduce**
I am trying to store a complete list of TargetCommunityPeople(name of my model) in a box. While storing it everything work fine(both store and retrieve),
when I close the box or restart my app, my box shows all the string or int data that I stored but my List will go with no error(only return it null).

this picture is the first time I store the model,

![Screenshot (17)](https://user-images.githubusercontent.com/47780958/118458764-4fbbce80-b710-11eb-87b1-9c996866a296.png)

and after restarting my app all of my stored data gone except string and int.

![Screenshot (18)](https://user-images.githubusercontent.com/47780958/118458916-72e67e00-b710-11eb-93bd-15db07009b1c.png)

**Code sample**

mission_model.dart
```

part 'mission_model.g.dart';

@HiveType(typeId: 1)
class MissionModel extends HiveObject {
@HiveField(0)
int id;
@HiveField(1)
int missionId;
@HiveField(2)
String workFlowDetailEngName;
@HiveField(3)
String mode;
@HiveField(4)
String title;
@HiveField(5)
List surveyKeys;
@HiveField(6)
List targetCommunityPeople;
@HiveField(7)
List surveys;

MissionModel({
this.id,
this.title,
this.missionId,
this.workFlowDetailEngName,
this.mode,
this.surveyKeys,
this.targetCommunityPeople,
this.surveys,

});
factory MissionModel.fromJson(Map json) {
List targetCommunityPeople = json['targetCommunityPeople'];
List targetCommunityPeopleList = [];

targetCommunityPeople.forEach((val) {
targetCommunityPeopleList.add(TargetCommunityPeople.fromJson(val));
});

List survey = json['surveys'];
List surveyModel = [];
survey.forEach((val) {
surveyModel.add(SurveyModel.fromJson(val));
});

List surveyKeys = json['surveyKeys'];
List surveyKeysModel = [];
surveyKeys.forEach((val) {
surveyKeysModel.add(SurveyKey.fromJson(val));
});

return MissionModel(
id: json['id'],
missionId: json['missionId'],
workFlowDetailEngName: json['workFlowDetailEngName'],
mode: json['mode'],
title: json['title'],
surveyKeys: surveyKeysModel,
targetCommunityPeople: targetCommunityPeopleList,
surveys: surveyModel,
);
}
}

```

mission_model.g.dart
```
part of 'mission_model.dart';

class MissionModelAdapter extends TypeAdapter {
@override
final int typeId = 1;

@override
MissionModel read(BinaryReader reader) {
final int numOfFields = reader.readByte();
final Map fields = {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};

return MissionModel()
..id = fields[0] as int
..missionId = fields[1] as int
..workFlowDetailEngName = fields[2] as String
..mode = fields[3] as String
..title = fields[4] as String
..surveyKeys = fields[5] as List
..targetCommunityPeople = fields[6] as List
..surveys = fields[7] as List;
}

@override
void write(BinaryWriter writer, MissionModel obj) {
writer
..writeByte(5)
..writeByte(0)
..write(obj.id)
..writeByte(1)
..write(obj.missionId)
..writeByte(2)
..write(obj.workFlowDetailEngName)
..writeByte(3)
..write(obj.mode)
..writeByte(4)
..write(obj.title)
..writeByte(5)
..write(obj.surveyKeys)
..writeByte(6)
..write(obj.surveys)
..writeByte(7)
..write(obj.targetCommunityPeople);
}

@override
int get hashCode => typeId.hashCode;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is MissionModelAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}

```

I open all my boxes in main.dart so no worries for open and registerAdapter.

and how I store my mission(list contain 3 mission as I send in pictures)
```
var missionBox = Hive.box('MISSION_MODEL');
list.forEach(
(val) {
MissionModel missionModel = MissionModel.fromJson(val);
missionList.add(missionModel);
missionBox.add(missionModel);
},
);
```

I can send other model and method if required,
and don't know if I need to use HiveList in this example and also could not find an example for HiveList that makes sense.

Thanks for your attention.

**Version**
- Platform: Android Emulator, Windows 10
- Flutter: 2.0.6
- hive_flutter: 1.0.0
- hive: 2.0.4

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.