isar / isar/hive

Hey, my data structure is probably like this, List<List<Model>>, so I can't get the key of the Model. What is going on?

Open
#665 0 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

**Code sample**
This is my entity class
```dart
import 'package:hive/hive.dart';

///
/// Code generated by jsonToDartModel https://ashamp.github.io/jsonToDartModel/
///
@HiveType()
class HealthModelResultsParts extends HiveObject {

String key2;
String value;
bool status;

HealthModelResultsParts({
this.key2,
this.value,
this.status,
});

HealthModelResultsParts.fromJson(Map json) {
key2 = json["key"]?.toString();
value = json["value"]?.toString();
status = json["status"];
}

Map toJson() {
final Map data = Map();
data["key"] = key2;
data["value"] = value;
data["status"] = status;
return data;
}
}

class HealthModelResultsPartsAdapter
extends TypeAdapter {
@override
HealthModelResultsParts read(BinaryReader reader) {
return HealthModelResultsParts(
key2: reader.read(),
value: reader.read(),
status: reader.read(),
);
}

@override
final int typeId = 0;

@override
void write(BinaryWriter writer, HealthModelResultsParts obj) {
writer.write(obj.key2);
writer.write(obj.value);
writer.write(obj.status);
}
}

@HiveType()
class HealthModelResults extends HiveObject {

String title;
List range;
List> parts;

HealthModelResults({
this.title,
this.range,
this.parts,
});

HealthModelResults.fromJson(Map json) {
title = json["title"]?.toString();
if (json["range"] != null) {
final v = json["range"];
final arr0 = [];
v.forEach((v) {
arr0.add(v.toInt());
});
range = arr0;
}
if (json["parts"] != null) {
final v = json["parts"];
final arr0 = >[];
v.forEach((v) {
final arr1 = [];
v.forEach((v) {
arr1.add(HealthModelResultsParts.fromJson(v));
});
arr0.add(arr1);
});
parts = arr0;
}
}

Map toJson() {
final Map data = Map();
data["title"] = title;
if (range != null) {
final v = range;
final arr0 = [];
v.forEach((v) {
arr0.add(v);
});
data["range"] = arr0;
}
if (parts != null) {
final v = parts;
final arr0 = [];
v.forEach((v) {
final arr1 = [];
v.forEach((v) {
arr1.add(v.toJson());
});
arr0.add(arr1);
});
data["parts"] = arr0;
}
return data;
}
}

class HealthModelResultsAdapter extends TypeAdapter {
@override
HealthModelResults read(BinaryReader reader) {
var title = reader.read();
var range = reader.read();
var parts = (reader.read() as List)
.cast>()
.map((e) => e.cast())
.toList();
return HealthModelResults(title: title, range: range, parts: parts);
}

@override
final int typeId = 1;

@override
void write(BinaryWriter writer, HealthModelResults obj) {
writer.write(obj.title);
writer.write(obj.range);
writer.write(obj.parts);
}
}

@HiveType()
class HealthModel extends HiveObject {

List results;

HealthModel({
this.results,
});

HealthModel.fromJson(Map json) {
if (json["results"] != null) {
final v = json["results"];
final arr0 = [];
v.forEach((v) {
arr0.add(HealthModelResults.fromJson(v));
});
results = arr0;
}
}

Map toJson() {
final Map data = Map();
if (results != null) {
final v = results;
final arr0 = [];
v.forEach((v) {
arr0.add(v.toJson());
});
data["results"] = arr0;
}
return data;
}
}

class HealthModelAdapter extends TypeAdapter {
@override
HealthModel read(BinaryReader reader) {
return HealthModel(results: reader.read().cast());
}

@override
final int typeId = 2;

@override
void write(BinaryWriter writer, HealthModel obj) {
writer.write(obj.results);
}
}

```

## This is the code I used to initialize hive

```dart
import 'dart:io';

import 'package:flutter_parenting/hive_model/health_model.dart';
import 'package:flutter_parenting/models/baby_info.dart';
import 'package:flutter_parenting/models/feeding_model2.dart';
import 'package:flutter_parenting/models/feeding_reminder.dart';
import 'package:flutter_parenting/models/health_model.dart';
import 'package:flutter_parenting/models/physical_model.dart';
import 'package:flutter_parenting/models/sleep_model.dart';
import 'package:flutter_parenting/models/vaccine_model.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:path_provider/path_provider.dart';

class HiveDBUtil {

static HiveDBUtil instance;

Box tagsBox;

Box healthContent;

Box feedingBox;

Box sleepBox;

Box babyInfoBox;

Box healthBox;

Box vaccineBox;

Box physicalBox;


///
static Future install() async {
Directory document = await getApplicationDocumentsDirectory();
Hive.init(document.path);

// Hive.registerAdapter(PartsModelAdapter());
Hive.registerAdapter(HealthModelResultsPartsAdapter());
Hive.registerAdapter(HealthModelResultsAdapter());
Hive.registerAdapter(HealthModelAdapter());
Hive.registerAdapter(FeedingReminderAdapter());
Hive.registerAdapter(SleepModelAdapter());
Hive.registerAdapter(SleepModelResultsAdapter());
Hive.registerAdapter(BabyInfoAdapter());

///Feeding
Hive.registerAdapter(FeedingModel2Adapter());
Hive.registerAdapter(FeedingModel2ResultsContentsAdapter());
Hive.registerAdapter(FeedingModel2ResultsAdapter());
//// Vaccine
Hive.registerAdapter(VaccineModelResultsVaccineAdapter());
Hive.registerAdapter(VaccineModelResultsAdapter());
Hive.registerAdapter(VaccineModelApter());

/// Physical
Hive.registerAdapter(PhysicalModelResultAdapter());
Hive.registerAdapter(PhysicalModelAdapter());

}

/// Init Box
static Future getInstance() async {
if (instance == null) {
instance = HiveDBUtil();
await Hive.initFlutter();

/// tags
instance.tagsBox = await Hive.openBox('tags');

/// health
// instance.healthBox = await Hive.openBox('health');
/// healthContent
instance.healthContent = await Hive.openBox('healthContent');
// instance.todoBox = await Hive.openBox("parts");
/// feeding
instance.feedingBox = await Hive.openBox('feeding');

/// sleeps
instance.sleepBox = await Hive.openBox("sleeps");

/// babyInfo
instance.babyInfoBox = await Hive.openBox("babyInfo");

/// health
instance.healthBox = await Hive.openBox("healthBox");

/// vaccine
instance.vaccineBox = await Hive.openBox("vaccine");

/// physical
instance.physicalBox = await Hive.openBox("physical");

}

return instance;
}
}
```

insert code
```dart
Future getHealth() async {
if (hiveDBUtil == null || hiveDBUtil.healthContent == null) {
init();
} else {
var s = hiveDBUtil.healthContent.values;

if (s.isEmpty) {
String jsonString =
await rootBundle.loadString("assets/json/health_2.json");
Map map = json.decode(jsonString);
if (hiveDBUtil == null || hiveDBUtil.healthContent == null) {
init();
}
healthModel = HealthModel.fromJson(map);
healthModel.results.map((e) async {
var dd = await hiveDBUtil.healthContent.add(e);
print(dd);
}).toList();
}
}
}
```
As mentioned in the code above, I want to get the key of HealthModelResultsParts, but what I get is null, I can only get the key of HealthModelResults

**Version**
- Platform: iOS, Android, Mac, Windows, Linux, Web
- Flutter version: [e.g. 1.5.4]
- Hive version: [e.g. 0.5.0]

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.