Questions tagged [json-serializable]
102 questions
1
vote
0 answers
How to use json_serializable package for complex response models - Flutter
I have seen several examples on how to use json_serializable package to auto-generate response models. But almost all of these examples have simple response structure.
For example, have a look at the Json response for a student's data.
{
"name":…

pratik97179
- 133
- 1
- 2
- 12
1
vote
0 answers
Is there a way to ignore toJson for a field based on other fields in freezed class?
I managed to ignore fields adding includeIfNull: false and toJson: ignore writing ignore function as T? ignore(dynamic _) => null;.
What I want to achieve is to include field "id" only if "variants" is an empty array, otherwise include its…

Alex Tarana
- 11
- 2
1
vote
0 answers
Json serialization of nested objects dart
I am trying to generate fromJSON and toJson functions for my nested objects using the json_serializable library. But when I tried to use the generated functions, I got an error with deserialization of the nested object.
I think I understand how to…

Dmitrii Domrachev
- 13
- 3
1
vote
1 answer
json_serializable flutter parse and map with key connect with modal and mapper
Thais is my json format
"recommended": {
"section_title": "Recommended",
"section_sub_title": "Recommended",
"data": {
"Allahabad": [
{
"gym_id": "9",
…

Arun99178
- 11
- 2
1
vote
1 answer
Provide generic arguments to DocumentReferences in JsonConverter
I'm using json_serializable's JSONConverter interface to convert Firestore's DocumentReferences into valid JSON objects.
This is the JSON converter class. It tries to be generic:
class DocumentSerializer
implements…

Shockch4rge
- 468
- 4
- 10
1
vote
1 answer
Flutter: Not able to use nested models with Entities. (Entities, Models, Equatable, Flutter, Dart, json_serializable,Json Serialization)
I have two file for a single data i.e Entity and Model. To separate the entire logic from each other and to follow best practices.
Whenever I am trying to use nested Entities and parsing through @JsonSerializable it's showing me error.
Note: I have…

Khursheed Gaddi
- 13
- 5
1
vote
2 answers
Flutter json_serializable: fromJson returns null
the json I receive from the api looks like this:
{
"USD": [
{
"name": "something",
"value": 123
},
{
"name": "something else",
"value": 1234
}
],
"EUR": [
... same here
]
}
and my dart model looks…

Fi Li Ppo
- 107
- 11
1
vote
3 answers
json_serializable not generating code for final fields and getters
This is my class:
@JsonSerializable()
class Foo {
final int a = 0;
int get b => 42;
}
The generated code doesn't include any of the a or b field:
Foo _$FooFromJson(Map json) => Foo();
Map _$FooToJson(Foo…

iDecode
- 22,623
- 19
- 99
- 186
1
vote
1 answer
Dart null safety and parsing JSON to models with json_serializable
I have a very general question after almost one week of playing around with various tools to de-serialize JSON to Dart and being very frustrated about null safety. The json_serializable package which generates .fromJson and .toJson messages should…

ThommyB
- 1,456
- 16
- 34
1
vote
0 answers
Flutter Freezed/json_serializable - runtimeType is missing from generated json
For some reason the runtimeType is missing in the generated json of this Freezed class. It only happens sometimes. See image below:
This causes the switch case in the image to fall through to throw FallThroughError();, since runtimeType doesn't…

BeniaminoBaggins
- 11,202
- 41
- 152
- 287
1
vote
0 answers
Can't (de)serialize nested generic classes with Dart freezed and json_serializable
I got a class First defined as:
@freezed
class First with _$First {
@JsonSerializable(explicitToJson: true)
factory First({
required String a,
@BConverter() required T b,
}) = _First;
factory First.fromJson(Map

Nik.Circles
- 51
- 5
1
vote
0 answers
Laravel php multiple JsonSerializable
I have complex object Model, with composition of other objects, and I use JsonSerializable in each of them.
Let take simplified example:
class CarPark implements JsonSerializable
{
/** @var array|Car[] */
private array $cars;
...
public…

Mantaz
- 333
- 1
- 4
- 12
1
vote
2 answers
Dart json_serializable defaultValue for Map with Enum Value
My main issue is that i want to prevent the field value to get null when creating an object from parsing json.
The field is a Map.
Example code:
import 'package:json_annotation/json_annotation.dart';
enum Letter{ A,…

Chris_S
- 11
- 1
- 2
0
votes
1 answer
Validating Json in Dart JsonSerializable and Freezed
I'm using Freezed to create data classes, and to de-serialized them from a json (with the integration with JsonSerializable).
But sometimes the json are incomplete, and the fromJson method do not validate the json. So it throws a _TypeError error…

perojas3
- 35
- 5
0
votes
1 answer
How to convert an object to json using freezed in flutter
I have written a freezed data class.
@freezed
class GithubRepoDTO with _$GithubRepoDTO {
const GithubRepoDTO._();
const factory GithubRepoDTO({
required UserDTO owner,
required String name,
@JsonKey(fromJson: _fromJson) required…

Akhil
- 419
- 5
- 15