Is there any way to pass a HttpClient instance?
Hello! 👋
As we try to use the same HttpClient to keep keepalive connections, is there any way to pass the CachedNetworkImageProvider a HttpClient to use?
Thanks 🙇♂️
I really would like to know this, too. I think it is kind of an essential feature.
But it looks like a http-session is created for every single image (#555). That would be absolutely horrible 😑
I'll take you around the CacheManager files.
When creating the CacheManager you supply a config file in the constructor. The default IO config file uses a standard HttpFileService if nothing else is provided. That http fileservice only creates 1 http client if no other client is provided.
So what you can do is create a different CacheManager object that you supply a config with an HttpFileService that has your httpclient. At the moment you cannot configure this per request.
So like this:
import 'package:http/http.dart' as http;
final cacheManager = CacheManager(
Config(
'testCache',
fileService: HttpFileService(
httpClient: http.Client(),
),
),
);
I'll take you around the CacheManager files.
When creating the CacheManager you supply a config file in the constructor. The default IO config file uses a standard HttpFileService if nothing else is provided. That http fileservice only creates 1 http client if no other client is provided.
So what you can do is create a different CacheManager object that you supply a config with an HttpFileService that has your httpclient. At the moment you cannot configure this per request.
So like this:
import 'package:http/http.dart' as http; final cacheManager = CacheManager( Config( 'testCache', fileService: HttpFileService( httpClient: http.Client(), ), ), );
i'm sorry, i'm still not quite sure what you meant and where to apply said code. is it in main.dart? or do i have to manually fork the plugin? i'm using self signed certificate on my client so i need to pass the httpClient to the plugin so it can retrieve it successfully, instead of handshake certificate expired.
``the code ByteData bytes = await rootBundle.load('cert/....pem'); print("bytes ${bytes.lengthInBytes}"); SecurityContext clientContext = new SecurityContext() ..setTrustedCertificatesBytes(bytes.buffer.asUint8List());
httpClient = IOClient(HttpClient(context: clientContext)); response = await httpClient.post(uri,header,.......).. ``
and i'm stoopid bakka......
import 'package:flutter_cache_manager/flutter_cache_manager.dart'; CachedNetworkImage( cacheManager: CacheManager( Config( 'testCache', fileService: HttpFileService( httpClient: http, ), ), ), httpHeaders: { "Authorization":"bearer ${globVar.tokenRest.token}" }, )
Just a piece of copypasteable code:
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:http/http.dart' as http;
final cacheManager = CacheManager(
Config(
'cacheKey',
fileService: HttpFileService(
httpClient: http.Client(),
),
),
);
If using it as cacheManager makes CachedNetworkImage and CachedNetworkImageProvider always use the same http client, that's it 😄
If you think it's a good way to deal with it, thanks a lot and feel free to close the issue 🙇