Flutter Future<HttpClient> is not a substype of HttpClinet

1,089

Here is the solution

Create Http Client before dio code :

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

Then call your dioAdapterCode :

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

Finaly your method would be :

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

Note: Issue was use of async/await in the code.

Let me know if the issue persists and support the answer

Share:
1,089
Durgesh Singh
Author by

Durgesh Singh

Updated on December 14, 2022

Comments

  • Durgesh Singh
    Durgesh Singh over 1 year
    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');
    
      }
    }
    

    In flutter we are getting using Dio. I am getting a error using Dio The problem is here that i need to use SSl certificate So i used Dio But now i am getting the below error. Unhandled exception Future is not a subtype of HttpClient.

    • pskink
      pskink over 4 years
      post the code where you use Future<Builder>, also post the stacktrace you are getting
    • Durgesh Singh
      Durgesh Singh over 4 years
      please find here above full code @pskink just edit the full code.
    • Andrey Turkovsky
      Andrey Turkovsky over 4 years
      I don't see that you return something from _getPosts method
    • Hardy
      Hardy over 4 years
      giving me same issue as well any luck with that?