Unverified Commit 35b55d51 authored by xster's avatar xster Committed by GitHub

Add image stream error handling mechanism (#18424)

parent e9c8e36b
...@@ -55,7 +55,7 @@ ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size si ...@@ -55,7 +55,7 @@ ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size si
/// Prefetches an image into the image cache. /// Prefetches an image into the image cache.
/// ///
/// Returns a [Future] that will complete when the first image yielded by the /// Returns a [Future] that will complete when the first image yielded by the
/// [ImageProvider] is available. /// [ImageProvider] is available or failed to load.
/// ///
/// If the image is later used by an [Image] or [BoxDecoration] or [FadeInImage], /// If the image is later used by an [Image] or [BoxDecoration] or [FadeInImage],
/// it will probably be loaded faster. The consumer of the image does not need /// it will probably be loaded faster. The consumer of the image does not need
...@@ -65,17 +65,38 @@ ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size si ...@@ -65,17 +65,38 @@ ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size si
/// The [BuildContext] and [Size] are used to select an image configuration /// The [BuildContext] and [Size] are used to select an image configuration
/// (see [createLocalImageConfiguration]). /// (see [createLocalImageConfiguration]).
/// ///
/// The `onError` argument can be used to manually handle errors while precaching.
///
/// See also: /// See also:
/// ///
/// * [ImageCache], which holds images that may be reused. /// * [ImageCache], which holds images that may be reused.
Future<Null> precacheImage(ImageProvider provider, BuildContext context, { Size size }) { Future<Null> precacheImage(
ImageProvider provider,
BuildContext context, {
Size size,
ImageErrorListener onError,
}) {
final ImageConfiguration config = createLocalImageConfiguration(context, size: size); final ImageConfiguration config = createLocalImageConfiguration(context, size: size);
final Completer<Null> completer = new Completer<Null>(); final Completer<Null> completer = new Completer<Null>();
final ImageStream stream = provider.resolve(config); final ImageStream stream = provider.resolve(config);
void listener(ImageInfo image, bool sync) { void listener(ImageInfo image, bool sync) {
completer.complete(); completer.complete();
} }
stream.addListener(listener); void errorListener(dynamic exception, StackTrace stackTrace) {
completer.complete();
if (onError != null) {
onError(exception, stackTrace);
} else {
FlutterError.reportError(new FlutterErrorDetails(
context: 'image failed to precache',
library: 'image resource service',
exception: exception,
stack: stackTrace,
silent: true,
));
}
}
stream.addListener(listener, onError: errorListener);
completer.future.then((Null _) { stream.removeListener(listener); }); completer.future.then((Null _) { stream.removeListener(listener); });
return completer.future; return completer.future;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment