JokingWolf
JokingWolf

Reputation: 83

Flutter Image.network(...) throws HandshakeException

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.

Upvotes: 8

Views: 17527

Answers (4)

Kiax
Kiax

Reputation: 756

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());
}

Upvotes: 24

Sheikh Shofiullah
Sheikh Shofiullah

Reputation: 326

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());
}

Upvotes: 2

Shyju M
Shyju M

Reputation: 9943

This is working fine for me

var image = new Container(
    width: 100.0,
    height: 100.0,
    decoration: new BoxDecoration(
      borderRadius: new BorderRadius.circular(3.0),
      color: const Color(0xff7c94b6),
      image: new DecorationImage(
        image: new NetworkImage(
            "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRwlzVkvBV1EA_w87NFvYAhT-EC2HMRpfTuRFtHE7nXE5GPvnsQ"),
        fit: BoxFit.cover,
      ),
    ),
  );

please do check this answer as well to add badCertificateCallback

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

Upvotes: 0

Alexander Doroshko
Alexander Doroshko

Reputation: 1851

Are you on Windows and using Kaspersky antivirus? I do not know all the technical details but this antivirus somehow affects the handshake. Disabling it should help. A similar issue is filed here, though I guess it's not a Dart SDK issue: https://github.com/dart-lang/sdk/issues/32131

Upvotes: 0

Related Questions