Home > Software design >  DioError [DioErrorType.other]: Converting object to an encodable object failed: Instance of 'Db
DioError [DioErrorType.other]: Converting object to an encodable object failed: Instance of 'Db

Time:12-08

Flutter , Dart , Dio , JsonSerialization

The code block where I get the error:

Response response = await Dio().post("https://10.0.2.2:44301/api/Company/companies", data: dbApiClientRequest);

Client Request Data to api

import 'package:json_annotation/json_annotation.dart';
import 'package:lotostandart_mobile/1.common/concrete/dtos/database_parameters.dart';

part 'db_api_client_request.g.dart';

@JsonSerializable(
    genericArgumentFactories: true,
    fieldRename: FieldRename.snake)
class DbApiClientRequest<TEntity,TFilter> { //,
  @JsonKey(name: "EntityList")
  List<TEntity> EntityList = <TEntity>[];

  @JsonKey(name: "FilterList")
  List<TFilter> FilterList = <TFilter>[];

  @JsonKey(name: "DatabaseParameters_")
  DatabaseParameters DatabaseParameters_ = DatabaseParameters();

  @JsonKey(name: "ClientType")
  String ClientType = "m";

  @JsonKey(name: "CurrentUserId")
  int CurrentUserId = -1;

  @JsonKey(name: "CurrentViewId")
  int CurrentViewId = -1;

   @JsonKey(name: "NextViewId")
  int NextViewId = -1;

  DbApiClientRequest();

  factory DbApiClientRequest.fromJson(Map<String, dynamic> json, TEntity Function(Object? json) fromJsonTEntity,TFilter Function(Object? json) fromJsonTFilter)
  => _$DbApiClientRequestFromJson(json, fromJsonTEntity,fromJsonTFilter);

  Map<String, dynamic> toJson(Object Function(TEntity value) toJsonTEntity,Object Function(TFilter value) toJsonTFilter)
  => _$DbApiClientRequestToJson(this, toJsonTEntity,toJsonTFilter);
}

ApiService Response Data to api

import 'package:json_annotation/json_annotation.dart';

part 'db_api_service_response.g.dart';

@JsonSerializable(
genericArgumentFactories: true,
  fieldRename: FieldRename.snake,
)
class DbApiServiceResponse<TEntity> {
  @JsonKey(name: "Entity")
  List<TEntity> Entity = <TEntity>[];

  @JsonKey(name: "Page")
  int Page = -1;

  @JsonKey(name: "PageSize")
  int PageSize = -1;

  @JsonKey(name: "ItemCount")
  int ItemCount = -1;

  @JsonKey(name: "TotalItemCount")
  int TotalItemCount = -1;

  @JsonKey(name: "Success")
  bool Success = false;

  @JsonKey(name: "Next")
  bool Next = false;

  @JsonKey(name: "Message")
  String Message = "";

  @JsonKey(name: "InternalMessage")
  String InternalMessage = "";

  @JsonKey(name: "StatusCode")
  int StatusCode = -1;

  @JsonKey(name: "HttpCode")
  int HttpCode = -1;

  @JsonKey(name: "LinkHref")
  String LinkHref = "";

  DbApiServiceResponse();

  factory DbApiServiceResponse.fromJson(Map<String, dynamic> json, TEntity Function(Object? json) fromJsonTEntity)
  => _$DbApiServiceResponseFromJson(json, fromJsonTEntity);

  Map<String, dynamic> toJson(Object Function(TEntity value) toJsonTEntity)
  => _$DbApiServiceResponseToJson(this, toJsonTEntity);
}

My Service Function

Future<DbApiServiceResponse<Company>> SendReceive(DbApiClientRequest<Company, CompanyRegistration> dbApiClientRequest) async {
  late DbApiServiceResponse<Company> serviceResponse;
  try {
    print("$connectionString/Company/companies");
    Response response = await Dio().post("https://10.0.2.2:44301/api/Company/companies", data: dbApiClientRequest);
    print(response.statusCode);
    if (response.statusCode == 200) {
      serviceResponse = DbApiServiceResponse<Company>.fromJson(response.data, ((items) => Company.fromJson(items as Map<String, dynamic>)));
      return serviceResponse;
    }
    return serviceResponse = DbApiServiceResponse<Company>();
  } on Exception catch (e) {
    return serviceResponse = DbApiServiceResponse<Company>();
  }
}

When I try with Postman, I can reach the api, but I cannot reach the api from flutter.

The error I got: DioError [DioErrorType.other]: Converting object to an encodable object failed: Instance of 'DbApiClientRequest<Company, CompanyRegistration>'

CodePudding user response:

enter image description here

You can see the image for the code.

CodePudding user response:

I solved the problem. I changed the code below one below.

Before,

Response response = await Dio().post("$connectionString/Company/companies",
data: dbApiClientRequest);

Then,

Response response = await Dio().post("$connectionString/Company/companies",
data: dbApiClientRequest.toJson((valueT) => valueT.toJson(), (valueF) => valueF.toJson()));
  • Related