'dio errror no response : Http status error [400]

DioError [DioErrorType.response]: Http status error [400]

But when i send data manually without using textcontroller Text it works. Please help me to fix this

Working perfectly in POSTMAN

class DioHelper
{
  static Dio? dio;
  static init ()
  {
    dio=Dio(
      BaseOptions(
          baseUrl: 'https://10.0.2.2:44385',
          receiveDataWhenStatusError: true
      ),
    );

  }
  static Future<Response?> getData({required url})async
  {
    return await dio?.get
      (
      url,
    ).then((value)
          {
            productResponse=ProductResponse.fromJson(value.data);
          }
          ).catchError((error)
          {
            print(error.toString());
          });
  }

}
class MyHttpOverrides extends HttpOverrides{
  @override
  HttpClient createHttpClient(SecurityContext? context){
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
  }

i use float action point to print the data in terminal


floatingActionButton: FloatingActionButton(

        onPressed: (){
          DioHelper.getData(
              url: '/Products/GetAllProducts',
          ).then((value)
          {print(
              value?.data.toString());

          }).catchError((error){print(error.toString());
          });
        },
      ),

the error that i get when i try run it i use emulitor

enter image description here

enter image description here



Solution 1:[1]

You should add interceptor to check messsage from backend, which cause you the error:

dio = Dio(
      BaseOptions(
          baseUrl: 'https://10.0.2.2:44385',
          receiveDataWhenStatusError: true
      ),
    )..interceptors.add(LogInterceptor(responseBody: true, requestBody: true));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 manhtuan21