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
Author by
Durgesh Singh
Updated on December 14, 2022Comments
-
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 over 4 yearspost the code where you use
Future<Builder>
, also post the stacktrace you are getting -
Durgesh Singh over 4 yearsplease find here above full code @pskink just edit the full code.
-
Andrey Turkovsky over 4 yearsI don't see that you return something from
_getPosts
method -
Hardy over 4 yearsgiving me same issue as well any luck with that?
-