Hello, I'm currently having an issue with json serializer not converting my data classes to json
I created a class like this
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:meta/meta.dart';
import 'package:json_annotation/json_annotation.dart';
import 'flags.dart';
part 'joke.freezed.dart';
part 'joke.g.dart';
@immutable
@freezed
abstract class Joke with _$Joke {
const factory Joke({
int id,
String category,
String type,
String setup,
String delivery,
String joke,
Flags flags,
bool error,
}) = _Joke;
const factory Joke.single({
int id,
@required String category,
@required String type,
@required String joke,
@required Flags flags,
bool error,
}) = SingleJoke;
const factory Joke.twoPart({
int id,
@required String category,
@required String type,
@required String setup,
@required String delivery,
@required Flags flags,
bool error,
}) = TwoPartJoke;
factory Joke.fromJson(Map<String, dynamic> json) => _$JokeFromJson(json);
}
and ran
flutter pub run build_runner watch --delete-conflicting-outputs
and it generated this
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'joke.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_$_Joke _$_$_JokeFromJson(Map<String, dynamic> json) {
return _$_Joke(
id: json['id'] as int,
category: json['category'] as String,
type: json['type'] as String,
setup: json['setup'] as String,
delivery: json['delivery'] as String,
joke: json['joke'] as String,
flags: json['flags'] == null
? null
: Flags.fromJson(json['flags'] as Map<String, dynamic>),
error: json['error'] as bool,
);
}
Map<String, dynamic> _$_$_JokeToJson(_$_Joke instance) => <String, dynamic>{
'id': instance.id,
'category': instance.category,
'type': instance.type,
'setup': instance.setup,
'delivery': instance.delivery,
'joke': instance.joke,
'flags': instance.flags,
'error': instance.error,
};
_$SingleJoke _$_$SingleJokeFromJson(Map<String, dynamic> json) {
return _$SingleJoke(
id: json['id'] as int,
category: json['category'] as String,
type: json['type'] as String,
joke: json['joke'] as String,
flags: json['flags'] == null
? null
: Flags.fromJson(json['flags'] as Map<String, dynamic>),
error: json['error'] as bool,
);
}
Map<String, dynamic> _$_$SingleJokeToJson(_$SingleJoke instance) =>
<String, dynamic>{
'id': instance.id,
'category': instance.category,
'type': instance.type,
'joke': instance.joke,
'flags': instance.flags,
'error': instance.error,
};
_$TwoPartJoke _$_$TwoPartJokeFromJson(Map<String, dynamic> json) {
return _$TwoPartJoke(
id: json['id'] as int,
category: json['category'] as String,
type: json['type'] as String,
setup: json['setup'] as String,
delivery: json['delivery'] as String,
flags: json['flags'] == null
? null
: Flags.fromJson(json['flags'] as Map<String, dynamic>),
error: json['error'] as bool,
);
}
Map<String, dynamic> _$_$TwoPartJokeToJson(_$TwoPartJoke instance) =>
<String, dynamic>{
'id': instance.id,
'category': instance.category,
'type': instance.type,
'setup': instance.setup,
'delivery': instance.delivery,
'flags': instance.flags,
'error': instance.error,
};
instead of this something like this, __please take note of__ .toJson() __I added to the flagstoJson classes __
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'joke.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_$_Joke _$_$_JokeFromJson(Map<String, dynamic> json) {
return _$_Joke(
id: json['id'] as int,
category: json['category'] as String,
type: json['type'] as String,
setup: json['setup'] as String,
delivery: json['delivery'] as String,
joke: json['joke'] as String,
flags: json['flags'] == null
? null
: Flags.fromJson(json['flags'] as Map<String, dynamic>),
error: json['error'] as bool,
);
}
Map<String, dynamic> _$_$_JokeToJson(_$_Joke instance) => <String, dynamic>{
'id': instance.id,
'category': instance.category,
'type': instance.type,
'setup': instance.setup,
'delivery': instance.delivery,
'joke': instance.joke,
'flags': instance.flags.toJson(),
'error': instance.error,
};
_$SingleJoke _$_$SingleJokeFromJson(Map<String, dynamic> json) {
return _$SingleJoke(
id: json['id'] as int,
category: json['category'] as String,
type: json['type'] as String,
joke: json['joke'] as String,
flags: json['flags'] == null
? null
: Flags.fromJson(json['flags'] as Map<String, dynamic>),
error: json['error'] as bool,
);
}
Map<String, dynamic> _$_$SingleJokeToJson(_$SingleJoke instance) =>
<String, dynamic>{
'id': instance.id,
'category': instance.category,
'type': instance.type,
'joke': instance.joke,
'flags': instance.flags.toJson(),
'error': instance.error,
};
_$TwoPartJoke _$_$TwoPartJokeFromJson(Map<String, dynamic> json) {
return _$TwoPartJoke(
id: json['id'] as int,
category: json['category'] as String,
type: json['type'] as String,
setup: json['setup'] as String,
delivery: json['delivery'] as String,
flags: json['flags'] == null
? null
: Flags.fromJson(json['flags'] as Map<String, dynamic>),
error: json['error'] as bool,
);
}
Map<String, dynamic> _$_$TwoPartJokeToJson(_$TwoPartJoke instance) =>
<String, dynamic>{
'id': instance.id,
'category': instance.category,
'type': instance.type,
'setup': instance.setup,
'delivery': instance.delivery,
'flags': instance.flags.toJson(),
'error': instance.error,
};
And created a test case to test it. These were the results
Expected: {
'category': 'Miscellaneous',
'type': 'twopart',
'setup': 'What do you call a pile of kittens?',
'delivery': 'A meowntain.',
'flags': {
'nsfw': false,
'religious': false,
'political': false,
'racist': false,
'sexist': false
},
'id': 132,
'error': false
}
Actual: {
'id': 132,
'category': 'Miscellaneous',
'type': 'twopart',
'setup': 'What do you call a pile of kittens?',
'delivery': 'A meowntain.',
'flags': _$_Flags:Flags(nsfw: false, religious: false, political: false, racist: false, sexist: false),
'error': false,
'runtimeType': 'twoPart'
}
These are my dependency versions
Dart VM version: 2.8.0-dev.20.10 (beta)
Flutter 1.17.0-3.2.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision 2a7bc389f2 (5 days ago) • 2020-04-21 20:34:20 -0700
Engine • revision 4c8c31f591
Tools • Dart 2.8.0 (build 2.8.0-dev.20.10)
json_serializable: ^3.3.0
Thanks
I face the same issue and fix it by using:
@JsonSerializable(explicitToJson: true)
This is not a bug. toJson is not called by default. Use explicitToJson as @Zain4994 mentioned if you want this behavior!
Most helpful comment
I face the same issue and fix it by using:
@JsonSerializable(explicitToJson: true)