getImage static method
Get the image codec
This function gets the image codec of url
. It verifies if the image is
in cache and returns it if enableCache
is true. If the images is not in cache
then the function download the image and stores in cache if enableCache
is true.
Implementation
static Future<ui.Codec> getImage(String url, Duration retryDuration,
Duration maxRetryDuration, bool enableCache,
{bool clearCacheImage = false}) async {
Uint8List bytes;
String id = _stringToBase64.encode(url);
String path = _tempPath + '/' + id;
final File file = File(path);
if (clearCacheImage) {
file.deleteSync();
_cachedPaths.remove(path);
}
if (fileIsCached(file)) {
bytes = file.readAsBytesSync();
} else {
bytes = await downloadImage(url, retryDuration, maxRetryDuration);
if (bytes.lengthInBytes != 0) {
if (enableCache) {
saveFile(file, bytes);
}
} else {
/// TODO The image can't be downloaded
return ui.instantiateImageCodec(Uint8List(0));
}
}
return ui.instantiateImageCodec(bytes);
}