Reputation: 668
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 String description,
@JsonKey(name: 'stargazers_count') required int stargazersCount,
}) = _GithubRepoDTO;
factory GithubRepoDTO.fromJson(Map<String, dynamic> json) =>
_$GithubRepoDTOFromJson(json);
}
So I can write fromJson with freezed. But when I tried to have a toJson. I can't write it. So I used jsonSerializable, so now I can have a toJson, this is the updated code.
@freezed
@JsonSerializable()
class GithubRepoDTO with _$GithubRepoDTO {
const GithubRepoDTO._();
const factory GithubRepoDTO({
required UserDTO owner,
required String name,
@JsonKey(fromJson: _fromJson) required String description,
@JsonKey(name: 'stargazers_count') required int stargazersCount,
}) = _GithubRepoDTO;
factory GithubRepoDTO.toGithubRepoDTO(Map<String, dynamic> json) =>
_$GithubRepoDTOFromJson(json);
Map<String, dynamic> toJson() => _$GithubRepoDTOToJson(this);
}
Now I can have toJson, but in this way, the problem is now I need to name fromJson any other name , I cant use fromJson now, when i used it shows duplicated reference found on auto generated code. So here i renamed fromJson to toGithubRepoDTO.
Upvotes: 1
Views: 6821
Reputation: 111
Yesterday I also met with the problem of not being able to specify toJson
in my class and I managed to solve the problem.
Solution: you can use the toJson
method without describing it in your class, as you do for example with fromJson
, because it will be described in the class generated by freezed.
Applicable to your code, you need to do so in your class file:
@freezed
class GithubRepoDTO with _$GithubRepoDTO {
const factory GithubRepoDTO({
required UserDTO owner,
required String name,
required String description,
@JsonKey(name: 'stargazers_count') required int stargazersCount,
}) = _GithubRepoDTO;
factory GithubRepoDTO.fromJson(Map<String, dynamic> json) =>
_$GithubRepoDTOFromJson(json);
}
And the following is what you need to do in the files where you want to apply the toJson
method:
final GithubRepoDTO githubRepoDto = getGithubRepoDto(); // instead of this line, you can use another code to get or create a GithubRepoDTO instance
final Map<String, dynamic> githubRepoDtoJson = githubRepoDto.toJson();
So in fact, you just don't need to describe toJson
in your file, since this method is already in the file generated by freezed, and you can already use it.
Upvotes: 1
Reputation: 231
According to freezed documentation you need to put @JsonSerializable(explicitToJson: true)
inside the class since you are using nested freezed objects (e.g. UserDTO). Also, you need to implement your custom json converter for the _fromJson method.
class YourJsonConverter extends JsonConverter<String?, String?> {
const YourJsonConverter();
// TODO implement from/toJson
}
@freezed
class GithubRepoDTO with _$GithubRepoDTO {
const GithubRepoDTO._();
@JsonSerializable(explicitToJson: true)
const factory GithubRepoDTO({
required UserDTO owner,
required String name,
@YourJsonConverter() required String description,
@JsonKey(name: 'stargazers_count') required int stargazersCount,
}) = _GithubRepoDTO;
factory GithubRepoDTO.fromJson(Map<String, dynamic> json) => _$GithubRepoDTOFromJson(json);
}
Putting the @JsonSerializable()
annotation inside the class will give you invalid annotation target warning. However, this is a known issue about the package and you can disable it as its mentioned here
Now you can name it as fromJson since there are no duplicate fromJson methods in the generated code.
Upvotes: 0