isar / isar/hive

nested classes with hive

Open
#834 1 comment 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**
How we can write nested classes into hive. I have a class for gaining data from api. But how I should tranform it?

**Code sample**
```
import 'package:json_annotation/json_annotation.dart';

part 'beer_response.g.dart';

@JsonSerializable()
class BeerResponse {
final int id;
final String name;
@JsonKey(name: 'first_brewed')
final String firstBrewed;
final String description;
@JsonKey(name: 'image_url')
final String? imageUrl;
final double? abv;
final double? ibu;
final double? ebc;
final Ingredients ingredients;
@JsonKey(name: 'food_pairing')
final List foodPairing;

BeerResponse({
required this.id,
required this.name,
required this.firstBrewed,
required this.description,
this.imageUrl,
this.abv,
this.ibu,
this.ebc,
required this.ingredients,
required this.foodPairing,
});

factory BeerResponse.fromJson(Map json) =>
_$BeerResponseFromJson(json);

Map toJson() => _$BeerResponseToJson(this);
}

@JsonSerializable()
class Ingredients {
final List malt;
final List hops;
String yeast;

Ingredients({
required this.malt,
required this.hops,
required this.yeast,
});

factory Ingredients.fromJson(Map json) =>
_$IngredientsFromJson(json);

Map toJson() => _$IngredientsToJson(this);
}

@JsonSerializable()
class Malt {
final String name;
final Amount amount;

Malt({
required this.name,
required this.amount,
});

factory Malt.fromJson(Map json) => _$MaltFromJson(json);

Map toJson() => _$MaltToJson(this);
}

@JsonSerializable()
class Amount {
final double value;
final String unit;

Amount({
required this.value,
required this.unit,
});

factory Amount.fromJson(Map json) => _$AmountFromJson(json);

Map toJson() => _$AmountToJson(this);
}

@JsonSerializable()
class Hops {
final String name;
final Amount amount;
final String add;
final String attribute;

Hops({
required this.name,
required this.amount,
required this.add,
required this.attribute,
});

factory Hops.fromJson(Map json) => _$HopsFromJson(json);

Map toJson() => _$HopsToJson(this);
}

```

**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.