I am having the following UserDto class:
part 'user_dto.freezed.dart';
part 'user_dto.g.dart';
@freezed
abstract class UserDto implements _$UserDto {
const factory UserDto({
required String userId,
required String userName,
required String userImageUrl,
String? emailAddress,
}) = _UserDto;
factory UserDto.fromJson(Map<String, dynamic> json) =>
_$UserDtoFromJson(json);
const UserDto._();
factory UserDto.fromDomain(User user) {
return UserDto(
userId: user.id.getOrCrash(),
emailAddress: user.emailAddress.getOrCrash(),
userImageUrl: user.userImageUrl.getOrCrash(),
userName: user.userName.getOrCrash(),
);
}
}
and the following CommentDto class which uses the UserDto:
part 'comment_dto.freezed.dart';
part 'comment_dto.g.dart';
@freezed
abstract class CommentDto implements _$CommentDto {
const factory CommentDto({
required String id,
required String keekzId,
required String content,
required int likeCount,
@JsonKey(includeToJson: true) required UserDto author,
}) = _CommentDto;
factory CommentDto.fromDomain(Comment comment) {
return CommentDto(
content: comment.commentContent.getOrCrash(),
likeCount: comment.commentLikeCounter.getOrCrash(),
id: comment.id.getOrCrash(),
author: UserDto.fromDomain(comment.author),
keekzId: comment.keekzId.getOrCrash(),
);
}
//! From Json to Domain
factory CommentDto.fromJson(Map<String, dynamic> json) =>
_$CommentDtoFromJson(json);
const CommentDto._();
}
The problem is now, whenever I call the CommentDto.toJson() it just works shallow, so it will not execute the UserDto.toJson().
_$_CommentDto _$$_CommentDtoFromJson(Map<String, dynamic> json) =>
_$_CommentDto(
id: json['id'] as String,
keekzId: json['keekzId'] as String,
content: json['content'] as String,
likeCount: json['likeCount'] as int,
author: UserDto.fromJson(json['author'] as Map<String, dynamic>),
);
Map<String, dynamic> _$$_CommentDtoToJson(_$_CommentDto instance) =>
<String, dynamic>{
'id': instance.id,
'keekzId': instance.keekzId,
'content': instance.content,
'likeCount': instance.likeCount,
'author': instance.author,
};
I want to have the last row here to be like so that this is beign executed.
'author': instance.author.toJson(),
How can I achieve this?