-2

I'm trying to use dio on Flutter, but i have to ignore the certificate validation so I made some changes on code and i got the error:

E/flutter (21674): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'httpClientAdapter' was called on null.
E/flutter (21674): Receiver: null
E/flutter (21674): Tried calling: httpClientAdapter

Can someone help me fixing it? The function that i'm using:

  Future<void> _login3() async {

    Dio dio;
    if (Platform.isAndroid) {
      (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
          (client) {
        client.badCertificateCallback =
            (X509Certificate cert, String host, int port) => true;
        return client;
      };
    }

    Response response =
        await Dio().get("https://sistema.hutransportes.com.br/api/login.php");
    // Response response;

    response =
        await dio.post("/test", data: {"user": "renato", "password": "123456"});
    print(response.data.toString());

  }
abrev
  • 305
  • 4
  • 18

1 Answers1

1

This should be pretty easily solvable by stepping through your code. I suggest putting a breakpoint on the first line of this method and debugging before looking at the answer, it should be pretty obvious what's going on. The message of the exception also states specifically what went wrong.

You haven't newed up an an instance of Dio before trying to access properties on that variable. Dio dio creates the variable but the value is null. In the next line you try to access the httpClientAdapter property of the null value and it rightfully throws an exception. Fix this by changing your first line to Dio dio = Dio();

Samuel Huff
  • 688
  • 6
  • 12
  • Oh, thats it, thanks. But when i fixed this i got again the certificate error, "CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate", I guess i will never solve this. – abrev Apr 01 '20 at 18:24
  • There's a lot of other SO issues for this same problem. At least in my experience this has to do with the way your emulator is performing on the domain. [Here](https://stackoverflow.com/questions/52618330/android-emulator-not-connecting-to-localhost-api/52626534#52626534) and [here](https://stackoverflow.com/a/54359013/6425808) are some ways to get around the problem. – Samuel Huff Apr 01 '20 at 18:46