Skip to content

Instantly share code, notes, and snippets.

@gabcarneiro
Last active November 27, 2023 18:18
Show Gist options
  • Save gabcarneiro/02580611279c85179db015ad2c7ed936 to your computer and use it in GitHub Desktop.
Save gabcarneiro/02580611279c85179db015ad2c7ed936 to your computer and use it in GitHub Desktop.
CustomCacheManager
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
class CustomCacheManager extends BaseCacheManager {
static const key = 'custom_cache_manager';
static CustomCacheManager _instance;
factory CustomCacheManager() {
if (_instance == null) {
_instance = CustomCacheManager._();
}
return _instance;
}
CustomCacheManager._()
: super(
key,
fileService: CustomHttpFileService(),
);
Future<String> getFilePath() async {
var directory = await getApplicationSupportDirectory();
return p.join(directory.path, key);
}
}
class CustomHttpFileService extends HttpFileService {
CustomHttpFileService() : super();
@override
Future<FileServiceResponse> get(String url,
{Map<String, String> headers = const {}}) {
if (url.startsWith('https')) {
url = url.replaceFirst('https', 'http');
}
return super.get(url, headers: headers);
}
}
class MyComponent extends StatelessWidget {
final String url;
const MyComponent({Key key, this.url}) : super(key: key);
@override
Widget build(BuildContext context) {
return CachedNetworkImage(
imageUrl: url,
cacheManager: CustomCacheManager(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment