How to implement @JsonKey(name: "users.job.job1")? (Access to nested json) #1150
Answered
by
rrousselGit
phamquoctrongdev
asked this question in
Q&A
-
Sorry for my bad English.
How to I create a freezed class like this:
@rrousselGit |
Beta Was this translation helpful? Give feedback.
Answered by
rrousselGit
Jan 21, 2025
Replies: 2 comments
-
There's no such thing. Don't type your field as |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
phamquoctrongdev
-
let's refer to this code implementation, freezed can not do that directly. you should use 2 different models for import 'package:freezed_annotation/freezed_annotation.dart';
part 'user.freezed.dart';
part 'user.g.dart';
@freezed
class User with _$User {
const factory User({
required Job job,
}) = _User;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}
@freezed
class Job with _$Job {
const factory Job({
@JsonKey(name: 'job_1') required String job1,
@JsonKey(name: 'job_2') required String job2,
}) = _Job;
factory Job.fromJson(Map<String, dynamic> json) => _$JobFromJson(json);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's no such thing. Don't type your field as
String
and type it asUser
instead.