Handling “Certification Verification Failed: Unable to Get Local Issuer Certificate” in Flutter

Ashish Sharma
Nerd For Tech
Published in
3 min readDec 16, 2023

When working with network requests or APIs in Flutter, encountering SSL/TLS-related issues, such as “Certification Verification Failed: Unable to Get Local Issuer Certificate,” can be quite frustrating. This issue frequently arises due to a disparity between the server’s SSL certificate presentation and the client’s ability to verify its authenticity. This error typically occurs due to certificate validation problems, particularly when the local system fails to recognize the certificate authority or issuer of the server’s SSL certificate. Let’s explore how to tackle and resolve this issue effectively.

Particularly if it works solely on certain devices, troubleshooting becomes more intricate.

Understanding the Issue

The error message points towards a failure in validating the SSL/TLS certificate provided by the server during the HTTPS connection. This often occurs because the client application doesn’t trust the certificate issuer or doesn’t have access to the necessary root certificate.

Resolving the Error

1. Addressing Certificate Trust Issues

One common cause of this error is the absence of a trusted certificate or failure to recognize the certificate authority that issued the server’s SSL certificate.

Solution: Ensure that the client application recognizes and trusts the certificate authority responsible for issuing the server’s SSL certificate. This might involve adding the root certificate of the issuer to the local trust store of the app.

2. Configuration of HTTP Client Libraries

Certain HTTP client libraries used in Flutter applications have specific configurations related to SSL certificate validation.

Solution:

  1. Download cert from LetsEncrypt or you can use your own valid certificate.pem
  2. Add this file to assets/ca/ Flutter project root directory

3. Add assets/ca/ assets directory in pubspec.yaml


flutter:
uses-material-design: true

assets:
- assets/ca/

4. Add this code on your app initialization:

void main() async {
WidgetsFlutterBinding.ensureInitialized();

ByteData data = await PlatformAssetBundle().load('assets/ca/lets-encrypt-r3.pem');
SecurityContext.defaultContext.setTrustedCertificatesBytes(data.buffer.asUint8List());

runApp(MyApp());
}

3. Considerations when Bypassing Certificate Validation

While not recommended due to security risks, some developers consider bypassing SSL certificate validation temporarily to avoid the error.

If all above solutions failed to work, then

Solution:

Conclusion

Resolving the “Certification Verification Failed: Unable to Get Local Issuer Certificate” error in Flutter requires meticulous attention to SSL/TLS certificate validation. By addressing certificate trust issues, exploring HTTP client configurations, and considering trusted server options, developers can effectively overcome this error and establish secure connections in their Flutter applications.

That’s it for this article! I hope you enjoyed it and leave a few claps if you did. Follow me for more Flutter articles and comment for any feedback you might have about this article.

I‘m Ashish Sharma aka theFlutterist. You can find me on LinkedIn or maybe follow me on GitHub as well.

Checkout my other articles:

--

--

Ashish Sharma
Nerd For Tech

TheFlutterist: Teaching Flutter in the most playful way possible. Join me in the land of widgets and hot reloads! http://flutterist.in/