getBytes method
Implementation
Future<Uint8List> getBytes(
String url, {
Encoding storeEncoding: utf8,
int? forceCache,
}) async {
Completer<Uint8List> completer = Completer<Uint8List>();
CacheEntry? entry;
// 1 memory cache first
if (useMemory) {
entry = await _cache.get(url);
if (entry != null && entry.isValid()) {
stats.hitMemory += 1;
completer.complete(entry.bytes);
return completer.future;
}
}
// 2 local file cache
entry = await load(url);
if (entry != null && entry.isValid()) {
if (useMemory) {
stats.missInMemory += 1;
_cache.set(url, entry).then((_) {
stats.bytesInMemory += entry!.length;
});
}
completer.complete(entry.bytes);
return completer.future;
}
assert(!completer.isCompleted);
final HttpClientResponse response = await loader(url);
if (response.statusCode != HttpStatus.ok)
throw Exception(
'HTTP request failed, statusCode: ${response.statusCode}, $url');
final Uint8List bytes = await consolidateHttpClientResponseBytes(response);
if (bytes.lengthInBytes == 0)
throw Exception('NetworkImage is an empty file: $url');
stats.bytesDownload += bytes.lengthInBytes;
completer.complete(bytes);
int? ttl = forceCache ?? cacheableSeconds(response);
if (ttl != null) {
store(
url,
CacheEntry(
url: url,
bytes: bytes,
ttl: ttl,
ctime: DateTime.now(),
),
encoding: storeEncoding,
);
} else {
print("filecache: not cached $url");
}
return completer.future;
}