retrofit.dart
retrofit.dart copied to clipboard
toJson will not add if we use freezed.
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:
- 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);
}
- 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);
- 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.