trevorwang / retrofit.dart

retrofit.dart is an dio client generator using source_gen and inspired by Chopper and Retrofit.
https://mings.in/retrofit.dart/
MIT License
1.08k stars 246 forks source link

Object not converted to Json #650

Open YSDC opened 8 months ago

YSDC commented 8 months ago

I updated many dependencies for an application developed in June, among them Retrofit.

Then, when I did a login to call to my server, I received no data. It appeared that Retrofit was not converting my object to json.

The code generated by RetrofitGenerator:

class _RestUser implements RestUser {
  _RestUser(
    this._dio, {
    this.baseUrl,
  }) {
    baseUrl ??= 'https://xxxxxxxxx.com';
  }

  final Dio _dio;

  String? baseUrl;

  @override
  Future<LoginResponse> login(LoginRequest request) async {
    const _extra = <String, dynamic>{};
    final queryParameters = <String, dynamic>{};
    final _headers = <String, dynamic>{};
    final _data = request;
    final _result = await _dio.fetch<Map<String, dynamic>>(_setStreamType<LoginResponse>(Options(
      method: 'POST',
      headers: _headers,
      extra: _extra,
    )
        .compose(
          _dio.options,
          '/api/user/login',
          queryParameters: queryParameters,
          data: _data),
        )
        .copyWith(
            baseUrl: _combineBaseUrls(
          _dio.options.baseUrl,
          baseUrl,
        ))));
    final value = LoginResponse.fromJson(_result.data!);
    return value;
  }

To make it work, I had to manually replace the line data: _data with data: _data.toJson(). Any Idea why?

I will add the implementation of my LoginRequest class to, and let me know if you need anything else to provide some help. Already a big thank you if you read my message :)

import 'package:freezed_annotation/freezed_annotation.dart';

part 'login_request.freezed.dart';
part 'login_request.g.dart';
// coverage:ignore-file

@freezed
class LoginRequest with _$LoginRequest {
  @JsonSerializable(explicitToJson: true, includeIfNull: false)
  const factory LoginRequest(
      {@JsonKey(name: 'auth0_token') required String auth0Token,
      @JsonKey(name: 'username') required String username,
      @JsonKey(name: 'phone') String? phone,
      @JsonKey(name: 'email') String? email,
      @JsonKey(name: 'fcm_token') String? fcmToken,
      @JsonKey(name: 'picture_type') required String pictureType,
      @JsonKey(name: 'picture_value') required String pictureValue,
      @JsonKey(name: 'os') String? os}) = _LoginRequest;

  factory LoginRequest.fromJson(Map<String, Object?> json) => _$LoginRequestFromJson(json);
}
emintolgahanpolat commented 8 months ago

https://pub.dev/packages/freezed#fromjsontojson try this.

leslmosnk commented 8 months ago

!!! EDIT I thought I had the same problem.

@freezed
class PushDeviceTransfer with _$PushDeviceTransfer {
  @JsonSerializable(explicitToJson: true)
  factory PushDeviceTransfer({
    required String token,
    required String? device,
  }) = _PushDeviceTransfer;

  factory PushDeviceTransfer.fromJson(Map<String, dynamic> json) =>
      _$PushDeviceTransferFromJson(json);
}

Didn't seem to generate_data = value.toJson() in my retrofit file, just _data = value.

This was easily fixed by making the freezed class a sealed class eg.

class PushDeviceTransfer with _$PushDeviceTransfer {}
// to
sealed class PushDeviceTransfer with _$PushDeviceTransfer {}
alexjane19 commented 7 months ago

Did you add this config to build.yaml

`global_options: freezed: runs_before:

mduccc commented 7 months ago

@alexjane19 It's worked!!!