How to solve CERTIFICATE_VERIFY_FAILED error while performing a http request in flutter ?

 ERROR:   HandshakeException: Handshake error in client (OS Error:  CERTIFICATE_VERIFY_FAILED: application verification failure(handshake.cc:393))

The `CERTIFICATE_VERIFY_FAILED` error in Flutter occurs when the SSL certificate verification process fails during an HTTP request. This error typically happens when the server's SSL certificate cannot be verified, which can be due to various reasons such as an expired certificate, a self-signed certificate, or an invalid certificate chain.


To solve the `CERTIFICATE_VERIFY_FAILED` error in Flutter, you can follow these steps:

In your main.dart file, add or import the following class:

 import 'dart:io';

 class MyHttpOverrides extends HttpOverrides{

  @override

  HttpClient createHttpClient(SecurityContext? context){

    return super.createHttpClient(context)

      ..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;

  }

}

In your main function, add the following line after the function definition:

 HttpOverrides.global = MyHttpOverrides();

Your main.dart should look like this

void main() {

 // Your code

 HttpOverrides.global = MyHttpOverrides();

  runApp(const ConsultationApp());

}

This comment was very helpful to pass through this matter, and please note that...


FAQs

1. What is the "CERTIFICATE_VERIFY_FAILED" error in Flutter?

The "CERTIFICATE_VERIFY_FAILED" error in Flutter occurs when the SSL certificate verification for an HTTP request fails, typically due to issues with the server's certificate.

2. How can I trust a self-signed certificate in Flutter?

To trust a self-signed certificate in Flutter, you can use the http package and set the badCertificateCallback to return true. However, exercise caution when using this approach in a production environment.

3. What should I do if the server's SSL certificate has expired?

If the server's SSL certificate has expired, you should update the certificate on the server to a valid and current one.

4. Why might the "CERTIFICATE_VERIFY_FAILED" error occur due to hostname mismatch?

This error can occur if the hostname in the SSL certificate doesn't match the one your Flutter app is trying to connect to. Ensure that the certificate matches the hostname or adjust your app's connection settings accordingly.

5. Is SSL certificate validation essential for secure communication?

Yes, SSL certificate validation is crucial for secure communication between your app and a server. It ensures that the server is legitimate and helps prevent man-in-the-middle attacks.

Post a Comment

Thank you

Previous Post Next Post