If you are using flutter with REST APIs, you may come across “CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate” issue.
Cause:
The issue happens mostly when you are trying to access an API and a error caused due to misconfiguration while sending all the root and intermediate certificates to the webserver correctly at the time of communication between the client and the server.
Solution:
The solution to this problem is “HttpOverrides“. Lets see how to solve the flutter issue –
Add below class in your main.dart file
1 2 3 4 5 6 7 |
class MyHttpOverrides extends HttpOverrides{ @override HttpClient createHttpClient(SecurityContext context){ return super.createHttpClient(context) ..badCertificateCallback = (X509Certificate cert, String host, int port)=> true; } } |
Add add below line in your main() method-
1 2 3 4 |
void main() { <strong>HttpOverrides.<em>global </em>= new MyHttpOverrides();</strong> runApp(MaterialApp(home: MyApp(),)); } |
With the above changes you may additionally come across below compilation error –
Error: The parameter ‘context’ of the method ‘MyHttpOverrides.createHttpClient’ has type ‘SecurityContext’, which does not match the corresponding type, ‘SecurityContext?’, in the overridden method, ‘HttpOverrides.createHttpClient’. ‘SecurityContext’ is from ‘dart:io’.
to remove above compilation error –
1 2 3 4 5 6 7 |
class MyHttpOverrides extends HttpOverrides{ @override HttpClient createHttpClient(SecurityContext<strong><span class="has-inline-color has-bright-red-color">?</span></strong> context){ return super.createHttpClient(context) ..badCertificateCallback = (X509Certificate cert, String host, int port)=> true; } } |
make note of the “?” symbol along with SecurityContext. It will solve your compilation error.
Try to reload your application and check, the API will go through successfully without If you are using flutter with REST APIs, you may come across “CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate” error.