eitanliu / dart_mappable_plugin

DartMappable(Flutter Json To Dart Converter)
https://plugins.jetbrains.com/plugin/21845
GNU General Public License v3.0
1 stars 1 forks source link

Using the mappable mixin method to generate beans as the body request parameter for Dio or Retrofit,It's not work #2

Open Clearlove2015 opened 2 weeks ago

Clearlove2015 commented 2 weeks ago

error log1: [🔔 Dio] LoginReq cannot be used to imply a default content-type, please set a proper content-type in the request.

error log2: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DioException [bad response]: This exception was thrown because the response has a status code of 400 and RequestOptions.validateStatus was configured to throw for this status code. The status code of 400 has the following meaning: "Client error - the request contains bad syntax or cannot be fulfilled" Read more about status codes at https://developer.mozilla.org/en-US/docs/Web/HTTP/Status In order to resolve this exception you typically have either to verify and fix your request code or you have to fix the server code.

0 DioMixin.fetch (package:dio/src/dio_mixin.dart:520:7)

#1 _ApiService.login (package:flutter_retrofit/retrofit/api_service.g.dart:53:21) #2 RetrofitUtils.login (package:flutter_retrofit/retrofit/retrofit_utils.dart:71:20) #3 _MyHomePageState.build.. (package:flutter_retrofit/main.dart:82:100) if use Map replace bean to @Body parameter, It's OK
eitanliu commented 1 week ago

The issue maybe related dart_mappable/issues/82, can you provide a sample project.

Clearlove2015 commented 1 week ago

The issue maybe related dart_mappable/issues/82, can you provide a sample project.

https://github.com/Clearlove2015/flutter_retrofit_test

eitanliu commented 1 week ago

Because by dart_mappable toJson return value defined is a String, but Retrofit requires a Map. This issue is exactly what is discussed in the above issues. If you want the return value to be a Map, you can open Setting -> Tools -> DartMappable Settings switch the generation format to Custom, or change implement to freezed or json_serializable.

Clearlove2015 commented 1 week ago

Because by dart_mappable toJson return value defined is a String, but Retrofit requires a Map. This issue is exactly what is discussed in the above issues. If you want the return value to be a Map, you can open Setting -> Tools -> DartMappable Settings switch the generation format to Custom, or change implement to freezed or json_serializable.

I have implemented freezed, json_serializable,dart_mappable_custom three methods, except for dart_mappable_mixin,only dart_mappable_mixin method use @Body with bean has error

eitanliu commented 1 week ago

The reason for the different return value definitions is given in https://github.com/schultek/dart_mappable/issues/82.
If you must use dart_mappable_mixin refer to the configuration in https://github.com/schultek/dart_mappable/issues/82#issuecomment-1536427459. I have not used this method.

Clearlove2015 commented 1 week ago

The reason for the different return value definitions is given in schultek/dart_mappable#82. If you must use dart_mappable_mixin refer to the configuration in schultek/dart_mappable#82 (comment). I have not used this method.

It' OK, thank you ! haha https://github.com/schultek/dart_mappable/issues/82#issuecomment-1628823193 https://github.com/schultek/dart_mappable/issues/82#issuecomment-1971866982 https://github.com/rydmike/mapper_issue

but this plugin mixin generated code need to update, eg: image

Do you have any better suggestions for generating code for the mixin plugin?

eitanliu commented 1 week ago

You can configure it to only rename toJson and toMap, so it won't affect the code generated by the plugin.

global_options:
  dart_mappable_builder:
    options:
      renameMethods:
        # fromJson: deserialize
        # fromMap: fromJson
        toJson: serialize
        toMap: toJson
Clearlove2015 commented 1 week ago

You can configure it to only rename toJson and toMap, so it won't affect the code generated by the plugin.

global_options:
  dart_mappable_builder:
    options:
      renameMethods:
        # fromJson: deserialize
        # fromMap: fromJson
        toJson: serialize
        toMap: toJson

This is a good idea,thank you!