1
import 'dart:io';    
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app_retrofit/example.dart';

class _HomepageState extends State<Homepage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: FutureBuilder(
          future: this._getPosts(),
          builder: (context, snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.none:
                return CircularProgressIndicator();
                break;
              case ConnectionState.waiting:
                return CircularProgressIndicator();
                break;
              case ConnectionState.active:
                return CircularProgressIndicator();
                break;
              case ConnectionState.done:
                return CircularProgressIndicator();
                break;

              default:
                return CircularProgressIndicator();
            }
          },),
      ),
    );
  }

  Future _getPosts() async {
    final dio= Dio();
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = await (client) async {
      SecurityContext securityContext=new SecurityContext();
      final ByteData crtData = await rootBundle.load('assets/server.crt');
      securityContext.setTrustedCertificatesBytes(crtData.buffer.asUint8List());
      final ByteData keyBytes = await rootBundle.load('assets/server.key');
      securityContext.usePrivateKeyBytes(keyBytes.buffer.asUint8List());
      return HttpClient(context: securityContext);
    };
    final client = RestClient(dio);
    client.login('mad***h@****.in', '88888');

  }
}

在颤振中,我们正在使用 Dio。我在使用 Dio 时遇到错误问题出在我需要使用 SSl 证书所以我使用了 Dio 但现在我收到以下错误。未处理的异常 Future 不是 HttpClient 的子类型。

4

1 回答 1

2

这是解决方案

在 dio 代码之前创建 Http Client:

SecurityContext clientContext = SecurityContext(withTrustedRoots: false);
var certificate =
    (await rootBundle.load("yourAssetCertificatePath"))
        .buffer
        .asInt8List();
clientContext.setTrustedCertificatesBytes(certificate);
HttpClient clientT = HttpClient(context: clientContext);

然后调用您的 dioAdapterCode :

 final dio= Dio();
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
  return clientT ;
};
final client = RestClient(dio);

最后你的方法是:

 Future _getPosts() async {
final dio= Dio();
 SecurityContext clientContext = SecurityContext(withTrustedRoots: false);
    var certificate =
        (await rootBundle.load("yourAssetCertificatePath"))
            .buffer
            .asInt8List();
    clientContext.setTrustedCertificatesBytes(certificate);
    HttpClient clientT = HttpClient(context: clientContext);
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =  (client)  {

  return clientT;
};
final client = RestClient(dio);
client.login('mad***h@****.in', '88888');
  }

注意: 问题是在代码中使用了 async/await。

让我知道问题是否仍然存在并支持答案

于 2020-01-02T08:59:04.350 回答