flutter_cached_network_image icon indicating copy to clipboard operation
flutter_cached_network_image copied to clipboard

Is there any way to pass a HttpClient instance?

Open rocboronat opened this issue 4 years ago • 24 comments

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 🙇‍♂️

rocboronat avatar Sep 06 '21 11:09 rocboronat

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 😑

PascalOtto avatar Sep 24 '21 13:09 PascalOtto

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

renefloor avatar Oct 01 '21 07:10 renefloor

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,.......).. ``

Jefry-nolastname avatar Oct 07 '21 08:10 Jefry-nolastname

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}" }, )

Jefry-nolastname avatar Oct 07 '21 09:10 Jefry-nolastname

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 🙇

rocboronat avatar Oct 07 '21 14:10 rocboronat