0

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?

SRel
  • 383
  • 1
  • 11
  • 1
    Are you not supposed to use `with` instead of `implements` for your freezed annotated classes? – julemand101 Aug 12 '23 at 16:30
  • @julemand101 is correct, and replace `with` with `implements` and run `build_runner` again. Freezed *will* generate `toJson` calls on nested freezed classes. – offworldwelcome Aug 12 '23 at 16:50
  • Still don't work :( Comparing it with here I am doing the same, however there the genereated code contains also the nested toJson https://github.com/ResoCoder/flutter-ddd-firebase-course/blob/master/lib/infrastructure/notes/note_dtos.dart – SRel Aug 12 '23 at 21:08

0 Answers0