Flutter Image.network(...) throws HandshakeException



我几天前已经开始与Flutter和Dart合作,到目前为止进展顺利。真的很不错的工具,但是对于该应用程序,我构建了我需要从Web服务器a的图片A,每当我试图使用new Image.network(URL)称呼它时,此例外都会被抛弃:

hernshakeException:

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

预先感谢任何人可以帮助我。

跳过SSL认证问题并解决Image.network(url)问题的一种方法是使用以下代码:

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

我遇到了同样的问题!能够解决IT应用程序(这是一个相关的问题,因此更好地在服务器端解决它!(SOL:本地添加用户信任证书!或跳过检查!我选择添加证书。将您的证书(用于特定域(添加为PubSec.yaml文件中的资产。(您可以收集它形成Web浏览器(

assets:
   - assets/raw/certificate.pem

然后在提出网络请求之前,在应用程序中的某个地方添加以下代码。例如,在主函数中。

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

这对我来说很好

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,
      ),
    ),
  );

请检查此答案以添加badcertificatecallback

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

您是否在Windows上使用Kaspersky Antivirus?我不知道所有的技术细节,但是这种防病毒软件会以某种方式影响握手。禁用它应该有帮助。在这里提出了类似的问题,尽管我想这不是DART SDK问题:https://github.com/dart-lang/sdk/sdk/issues/32131

最新更新