Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toJson will not add if we use freezed. #682

Open
b14cknc0d3 opened this issue Jun 11, 2024 · 2 comments
Open

toJson will not add if we use freezed. #682

b14cknc0d3 opened this issue Jun 11, 2024 · 2 comments

Comments

@b14cknc0d3
Copy link

b14cknc0d3 commented Jun 11, 2024

Describe the bug
toJson will not add if we use freezed.
and show this warning

YourModel must provide a `toJson()` method which return a Map.

To Reproduce
Steps to reproduce the behavior:

  1. create a model with freezed
@freezed
class FoodTagCreateModel with _$FoodTagCreateModel {
  const factory FoodTagCreateModel({
    required String name,
    required String description,
  }) = _FoodTagCreateModel;

  factory FoodTagCreateModel.fromJson(Map<String, dynamic> json) =>
      _$FoodTagCreateModelFromJson(json);

}
  1. generate a ResApi client that use that model and run build runner
@RestApi()
abstract class CrudApi {
  factory CrudApi(Dio dio, {String? baseUrl}) = _CrudApi;

  @POST('/api/v1/my-model')
  Future<void> createAModel(@Body() MyModel body);
  1. See error
MyModel must provide a `toJson()` method which return a Map.
It is programmer's responsibility to make sure the MyModel is properly serialized

Expected behavior
If body is not Map force to add toJson() whether it has toJson() or not.

@b14cknc0d3 b14cknc0d3 changed the title toJson will not add if we exclude .g.dart in analysis_options toJson will not add if we use freezed. Jun 11, 2024
@b14cknc0d3
Copy link
Author

@freezed
class FoodTagCreateModel with _$FoodTagCreateModel {
  const factory FoodTagCreateModel({
    required String name,
    required String description,
  }) = _FoodTagCreateModel;

  factory FoodTagCreateModel.fromJson(Map<String, dynamic> json) =>
      _$FoodTagCreateModelFromJson(json);
  @override
  Map<String, dynamic> toJson() => toJson();
}

Temporary solution.

@fryette
Copy link

fryette commented Jun 25, 2024

Duplicate.
Correct solution is:

  1. Add build.yaml with next snippet
  freezed:
   runs_before:
     - json_serializable 
  json_serializable: 
     runs_before:
      - retrofit_generator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants