Questions tagged [json-serializable]
102 questions
29
votes
2 answers
Freezed and json_serializable: How to use a custom converter
I want to add a custom converter to a freezed class like in this answer.
I tried it with this code:
@freezed
class NewsPost with _$NewsPost {
factory NewsPost({
@JsonKey(name: "date") @TimestampConverter() DateTime? date,
}) = _NewsPost;
…

Valentin Seehausen
- 665
- 1
- 8
- 10
7
votes
0 answers
Using a single Flutter model for both isar database and JsonSerializable
I am trying to use a single model for both storing the data locally using Isar, and also for using with Retrofit for REST API requests.
But Isar requires all the Linked classes to be defined with the datatype IsarLink where…

Jishnu Raj
- 170
- 3
- 16
7
votes
2 answers
Flutter json_serializable what if key can have different names
I am using json_serializable in Flutter to store a class in a file and read back from it. I am not posting the original class here for simplicity, but the principle is that half way through writing the app I decided that I wanted to change the…

ambstuc
- 71
- 1
- 2
7
votes
1 answer
how to properly build.yaml?
I just started to learn Flutter.
I use vscode as editor
I need to use json_serializable on my code.
I read https://pub.dev/packages/json_serializable and make my build.yaml just like what show there.
targets:
$default:
builders:
…

Bino Oetomo
- 571
- 1
- 10
- 23
7
votes
2 answers
Flutter: json_serializable ignore nullable fields instead of throwing an error
Suppose there are two models User and City
@JsonSerializable()
class User {
int id;
String name;
City? city;
List

Himanshu Arora
- 309
- 4
- 13
7
votes
3 answers
JsonConverter for unions generated by freezed package in dart
I'm trying to implement toJson/fromJson for a union generated by the freezed package. Let's say we have a class as following:
@freezed
abstract class Result with _$Result {
const factory Result.error(String message) = Error;
const factory…

gugge
- 918
- 1
- 11
- 17
6
votes
2 answers
How to use private constructor in json_serializable
I'm using a private constructor in my class, but the code generation fails with
The class Foo has no default constructor.
I'm using latest json_serializable: version i.e. 6.1.5:
@JsonSerializable()
class Foo {
final int count;
…

iDecode
- 22,623
- 19
- 99
- 186
6
votes
1 answer
Use dart json_serializable to map an object field to JSON flat keys (one to many mapping)
I have a dart object that includes a field of type Money, which itself is composed of amount and currency:
@JsonSerializable()
class Account {
final String id;
final String type;
final String subtype;
final String origin;
final String…

lfreire
- 103
- 9
6
votes
2 answers
Freezed class with enum property throws error upon trying to serialize
I have a freezed class that takes an enum in its constructor, but when trying to perform the jsonEncode method on this class, it fails with the following error:
The following JsonUnsupportedObjectError was thrown while handling a gesture:
Converting…

Luca Cras
- 61
- 1
- 3
5
votes
1 answer
Generated class from freezed creates duplicate FromJson method
I have a class which I am trying to use with Freezed, Json Serializable, and Hive. After running dart run build_runner build and generating the necessary classes, my compiler gives me the following error:
: Error: Can't use '_$FooBarFromJson'…

Flik
- 365
- 1
- 7
- 18
5
votes
1 answer
JsonSerializable and JsonKey issue after upgrade on Flutter Freezed model
My app was running ok but after pub upgrade --major-versions I am getting problems on all models.
Example model:
import 'package:app_220/models/Leads/LeadFieldModel.dart';
import 'package:flutter/foundation.dart';
import…

Osmany Becerra Gonzalez
- 139
- 1
- 7
5
votes
2 answers
json_serializable - Add a generic field to a freezed/json_serializable class
How do I make a Freezed object take a generic type? I want to do this:
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:vepo/src/entity_types/option_entity.dart';
part 'vegan_item_tag.freezed.dart';
part…

BeniaminoBaggins
- 11,202
- 41
- 152
- 287
5
votes
0 answers
Dart: how to use json_serializable library to create different subclasses from the json input
UML
I have to serialize json in order to create specific subclass from the Enum which is an attribute of one class that contains the abstract class target
class Test {
Outcome outcome;
TestType type;
}
abstract class Outcome {
String…

mattia rogledi
- 51
- 1
4
votes
1 answer
`Unhandled Exception: Invalid argument: Instance of '_$_Category'` While sending data to firestore form a freezed generated class
I created two model classes with freezed. One class is having parameter of other class inside it. The problem started when I try to send data to firestore. This following error appears.
E/flutter ( 6175): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)]…

Usama Karim
- 1,100
- 4
- 15
4
votes
0 answers
Flutter: Using JsonSerializable with inheritance with clean architecture
I am trying to follow the Clean Architecture via this YouTube tutorial and because of that I have a set of models objects in the data layer that extends the entities objects.
Since I have many models, some of which having many fields, I wish to use…

Mackovich
- 3,319
- 6
- 35
- 73