Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Image.network(...) throws HandshakeException

Tags:

flutter

dart

I have started working with flutter and dart a few days ago and it's going well so far. Really nice tool, but for the app, I'm building I need a picture from a webserver a whenever I'm trying to call it with new Image.network(URL) this exception is thrown:

HandshakeException:

Handshake error in client (OS Error: CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(ssl_cert.c:345)).

Thanks in advance if anyone could help me.

like image 232
JokingWolf Avatar asked Apr 03 '18 19:04

JokingWolf


2 Answers

A way to skip the problem of SSL certification and solve the Image.network(url) issue is to use the following code:

import 'dart:io';

class MyHttpOverrides extends HttpOverrides{
  @override
  HttpClient createHttpClient(SecurityContext context){
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
  }
}

void main(){
    HttpOverrides.global = new MyHttpOverrides();
    runApp(new MyApp());
}
like image 111
Kiax Avatar answered Oct 12 '22 21:10

Kiax


I got same issue ! Able to solve it app (It's a server related problem, so Better solve it on server side! ) sol: Add user trust certificate locally ! or skip checking ! I choose to add certificate. Add your certificate(for your specific domain) as an asset in your pubsec.yaml file. (You can collect it form web browser)

assets:
   - assets/raw/certificate.pem

Then add the following code somewhere in your application before making network requests. For example, in the main function.

void main() async{
 await WidgetsFlutterBinding.ensureInitialized();
  ByteData data = await 
  rootBundle.load('assets/raw/certificate.pem');
  SecurityContext context = SecurityContext.defaultContext;
  context.setTrustedCertificatesBytes(data.buffer.asUint8List());
  runApp(MyApp());
}
like image 30
Sheikh Shofiullah Avatar answered Oct 12 '22 20:10

Sheikh Shofiullah