Fixing CERTIFICATE_VERIFY_FAILED in Flutter Windows apps (without disabling SSL)
Your Flutter app talks to its API without problems on Android. Then you build the Windows desktop version, and every request fails with this:
HandshakeException: Handshake error in client (OS Error: CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:393)) The API opens fine in Chrome on the same machine. The certificate is valid, not expired, and issued by a well-known CA. So why does the app refuse it?
The most common answer online is to set badCertificateCallback to return true. That makes the error go away by switching off certificate checks entirely, which is the one thing HTTPS exists to do. This tutorial shows a better approach: find out why the handshake fails, then fix the actual cause.
Why the browser works and your app doesn't
On desktop, Flutter makes HTTPS requests through dart:io, which uses Dart's own TLS implementation (built on BoringSSL). It does not go through the browser, and it is less forgiving than one.
The biggest difference is missing intermediate certificates. A server is supposed to send its own certificate plus the intermediate certificates that link it to a trusted root. Many servers are set up to send only the first one. Browsers quietly cover for this. They cache intermediates they've seen before, and they can download a missing one using the address embedded in the certificate. Dart's TLS client does neither. If the chain is incomplete, the handshake fails with unable to get local issuer certificate.
So "it works in Chrome" tells you very little. It's worth checking the chain directly.
Step 1: find the actual cause
There are four common causes. Check them in this order.
Is the server sending the full chain?
Run this from any machine with OpenSSL. Git for Windows includes it, so it works from Git Bash:
openssl s_client -connect api.example.com:443 -servername api.example.com -showcerts Look at two things in the output:
- The certificate chain section. If you see only
0 s:(the server's certificate) and no1 s:entry, the intermediate is missing. - The last line.
Verify return code: 21 (unable to verify the first certificate)is the classic sign of a missing intermediate.Verify return code: 0 (ok)means the chain is fine and the problem is somewhere else.
An online checker such as SSL Labs' server test shows the same thing as "Chain issues: Incomplete".
A frequent culprit is a server configured with cert.pem (the server certificate alone) instead of fullchain.pem.
Is something on the network replacing certificates?
Corporate firewalls, proxies and some antivirus products inspect HTTPS traffic. They do it by swapping the server's certificate for one signed by their own root CA, which IT installs on company PCs so browsers trust it. Your app's TLS client may not trust that root.
To check, look at the issuer in the openssl output, or click the padlock in the browser. If the issuer is your company's firewall or antivirus vendor rather than a public CA, this is your cause. It also explains why the app works at home and fails in the office.
Is it a self-signed or internal certificate?
Dev and staging servers often use a self-signed certificate or one from a company's internal CA. No public trust store includes these, so the app will reject them unless you tell it otherwise.
Is the root certificate expired or untrusted?
This one is rarer, but it happens. In September 2021, Let's Encrypt's old root (DST Root CA X3) expired, and older clients that didn't have the newer root started failing overnight. If the chain looks complete but the root is old, check its expiry date.
Step 2: fix it, from best option to worst
Option 1: fix the server (best)
If the chain is incomplete, fix it on the server. Then every client works: your Flutter app, scripts, other services and older devices.
On Nginx, point ssl_certificate at the full chain file:
server { listen 443 ssl; server_name api.example.com; ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem; # not cert.pem ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem; } Reload Nginx and run the openssl command again. You should now see Verify return code: 0 (ok).
When the missing intermediate is the cause, this one-line change fixes the Windows build with no changes to the app.
Option 2: trust a specific CA in the app
If you can't change the server, for example with an internal CA or a corporate inspection proxy, tell the app to trust that one CA and keep the normal trusted roots too.
First, export the CA certificate in PEM format and add it to your assets:
# pubspec.yaml flutter: assets: - assets/certs/internal-ca.pem Then build an HTTP client whose security context trusts it:
import 'dart:io'; import 'package:flutter/services.dart'; import 'package:http/http.dart' as http; import 'package:http/io_client.dart'; Future<http.Client> createHttpClient() async { final caBytes = await rootBundle.load('assets/certs/internal-ca.pem'); // withTrustedRoots: true keeps the normal public roots as well. final context = SecurityContext(withTrustedRoots: true) ..setTrustedCertificatesBytes(caBytes.buffer.asUint8List()); return IOClient(HttpClient(context: context)); } Create the client once at startup and reuse it for every request.
If you use Dio, pass the same context through its adapter:
import 'dart:io'; import 'package:dio/dio.dart'; import 'package:dio/io.dart'; import 'package:flutter/services.dart'; Future<Dio> createDio() async { final caBytes = await rootBundle.load('assets/certs/internal-ca.pem'); final context = SecurityContext(withTrustedRoots: true) ..setTrustedCertificatesBytes(caBytes.buffer.asUint8List()); return Dio() ..httpClientAdapter = IOHttpClientAdapter( createHttpClient: () => HttpClient(context: context), ); } This trusts only the CA you shipped, not every certificate. If the CA is ever replaced, ship an app update with the new file.
Option 3: skip checks for local development only (last resort)
For a local dev server with a throwaway certificate, you can bypass the check. Limit it to debug builds and one host:
import 'dart:io'; import 'package:flutter/foundation.dart'; HttpClient createDevHttpClient() { return HttpClient() ..badCertificateCallback = (X509Certificate cert, String host, int port) { // Only in debug builds, and only for our local dev server. return kDebugMode && host == 'dev.local.example.com'; }; } In a release build kDebugMode is false, so the callback always rejects bad certificates.
What not to do
- Don't ship
badCertificateCallback = (_, __, ___) => true. It accepts any certificate from anyone. On public Wi-Fi or a compromised network, that means anyone in the middle can read and change your API traffic, including login tokens. - Don't apply a fix before you know the cause. Trusting a custom CA won't help with a missing intermediate, and fixing the server won't help with a corporate proxy. The
opensslcheck takes a minute and tells you which problem you have.
Conclusion
When a Flutter Windows app fails with CERTIFICATE_VERIFY_FAILED but the browser works:
- Run
openssl s_clientand check the chain and the verify code. - If the chain is incomplete, fix the server to send
fullchain.pem. - If it's a corporate or internal CA, trust that specific CA with
SecurityContext. - Bypass the check only in debug builds, and only for one host.
It's also worth adding an openssl s_client check to your deployment checklist, so an incomplete chain gets caught before a desktop client finds it.
Further reading: the SecurityContext class and HttpClient.badCertificateCallback in the Dart API docs, and the Dio documentation.