Freezed: Support for MyClassToJson()?

Created on 25 Mar 2020  路  8Comments  路  Source: rrousselGit/freezed

Does this package support class sserialization to json? I didn't find anything related in the README I only find MyClassFromJson(json) example.

When I try to call MyClassToJson(this) the IDE show me a error. In the generated file there is no code related to toJson feature. Does this package support it or it is jus a Bug?

question

Most helpful comment

I see, when we write fromJson it generated toJson behind the scenes as well, and there is no warning for having the extra toJson field on the @freezed class. Got it. This is not clear. Thanks for the help.

All 8 comments

I Can't find anything related to this in the link you provided.

Have you tried it? its giving me a error and looks like the toJson code is not being generated.

It does.
Here's one of the test of this package which test the toJson part

https://github.com/rrousselGit/freezed/blob/master/packages/freezed/test/json_test.dart

We're not seeing freezed generate toJson()

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:twic/models2/program_type.dart';
import 'package:twic/services/search_service/models/university_search_result.dart';

part 'onboarding_view_model.freezed.dart';
part 'onboarding_view_model.g.dart';

/// The data backing the Onboarding flow
/// [email]: TODO: document these fields
@freezed
abstract class OnboardingViewModel with _$OnboardingViewModel {
  @JsonSerializable()
  factory OnboardingViewModel({
    @required String email,
    @required ProgramType program,
    @required int graduationYear,
    @required String major,
    @required String minor,
    @required String residentialCollege,
    @required String graduateSchool,
    @required List<String> organizations,
    @required UniversitySearchResult university,
  }) = _OnboardingViewModel;

  factory OnboardingViewModel.fromJson(Map<String, dynamic> json) =>
      _$OnboardingViewModelFromJson(json);
  Map<String, dynamic> toJson() => _$OnboardingViewModelToJson(this);
}
The method '_$OnboardingViewModelToJson' isn't defined for the type 'OnboardingViewModel'.
Try correcting the name to the name of an existing method, or defining a method named '_$OnboardingViewModelToJson'.dart(undefined_method)

Screen Shot 2020-04-29 at 9 41 14 AM

Generated mixin

mixin _$OnboardingViewModel {
  String get email;
  ProgramType get program;
  int get graduationYear;
  String get major;
  String get minor;
  String get residentialCollege;
  String get graduateSchool;
  List<String> get organizations;
  UniversitySearchResult get university;

  Map<String, dynamic> toJson();
  $OnboardingViewModelCopyWith<OnboardingViewModel> get copyWith;
}

Why did you write a toJson? You shouldn't, it's fine for you

I see, when we write fromJson it generated toJson behind the scenes as well, and there is no warning for having the extra toJson field on the @freezed class. Got it. This is not clear. Thanks for the help.

Closing as toJson is correctly generated

Was this page helpful?
0 / 5 - 0 ratings