loadLocalOrNetworkImageBytes function
Stream<Uint8List>
loadLocalOrNetworkImageBytes({
- required LoadImageOption option,
- StreamController<
ImageChunkEvent> ? chunkEvents, - void evictImageAsync()?,
Loads local image bytes or cached network image bytes, using given option and chunkEvents.
If the image is downloaded, it will also be recorded to given CacheManager.
Implementation
Stream<Uint8List> loadLocalOrNetworkImageBytes({
required LoadImageOption option,
StreamController<ImageChunkEvent>? chunkEvents,
void Function()? evictImageAsync,
}) async* {
// 1. get file and url
io.File? file;
String? url;
if (!option.useFuture) {
file = option.file; // nullable
url = option.url; // nullable
} else {
file = await option.fileFuture; // nullable
url = await option.urlFuture; // nullable
}
// 2. check file and url validity
var useFile = false;
var useUrl = false;
if (file == null) {
if (url == null) {
var ex = const LoadImageException.bothNull();
option.onFileLoading?.call();
option.onFileLoaded?.call(ex);
throw ex; // => null file, null url
} else {
useUrl = true; // => null file, non-null url
}
} else {
var existed = false;
try {
existed = await file.exists();
} catch (_) {
if (option.fileMustExist) {
// throw exceptions only if fileMustExist flag is true
rethrow;
}
}
if (existed) {
useFile = true; // => valid file
} else if (option.fileMustExist) {
var ex = LoadImageException.notExistedFile(filepath: file.path);
option.onFileLoading?.call();
option.onFileLoaded?.call(ex);
throw ex; // => invalid file
} else {
if (url == null) {
var ex = LoadImageException.notExistedFileNullUrl(filepath: file.path);
option.onFileLoading?.call();
option.onFileLoaded?.call(ex);
throw ex; // => invalid file, null url
} else {
useUrl = true; // => invalid file, non-null url
}
}
}
// load local image
if (useFile) {
option.onFileLoading?.call();
yield* _loadLocalImageBytes(
file: file!,
chunkEvents: chunkEvents,
evictImageAsync: evictImageAsync,
onLoaded: option.onFileLoaded,
);
}
// load cached network image
if (useUrl) {
option.onUrlLoading?.call();
yield* _loadCachedNetworkImageBytes(
url: url!,
chunkEvents: chunkEvents,
evictImageAsync: evictImageAsync,
headers: option.headers,
cacheManager: option.cacheManager,
cacheKey: option.cacheKey,
maxWidth: option.maxWidth,
maxHeight: option.maxHeight,
asyncHeadFirst: option.asyncHeadFirst,
networkTimeout: option.networkTimeout,
onLoaded: option.onUrlLoaded,
);
}
}