Search code examples
flutterdart

HandshakeException: CERTIFICATE_VERIFY_FAILED: self signed certificate error


I trying to get the user's current location in my mobile app. so that, I am calling the getcitydetail api call....and i am getting this error "HandshakeException: CERTIFICATE_VERIFY_FAILED: self signed certificate error"

I tried to by-pass this error by temporarily bypass SSL verification by modifying the HTTP client to accept self-signed certificates, but getting this error.


Solution

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

    execute this before API called:

    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    // for os 7 override http badCertificateCallback
    if((androidInfo.version.sdkInt ?? 30) < 26) {
      HttpOverrides.global = new AppHttpOverrides();
    }
    
    

    using 'device_info_plus' package for getting os api level