precachePicture function

Future<void> precachePicture(
  1. PictureProvider provider,
  2. BuildContext? context, {
  3. Rect? viewBox,
  4. ColorFilter? colorFilterOverride,
  5. Color? color,
  6. BlendMode? colorBlendMode,
  7. PictureErrorListener? onError,
})

Prefetches an SVG Picture into the picture cache.

Returns a Future that will complete when the first image yielded by the PictureProvider is available or failed to load.

If the image is later used by an SvgPicture, it will probably be loaded faster. The consumer of the image does not need to use the same PictureProvider instance. The PictureCache will find the picture as long as both pictures share the same key.

The onError argument can be used to manually handle errors while precaching.

See also:

  • PictureCache, which holds images that may be reused.

Implementation

Future<void> precachePicture(
  PictureProvider provider,
  BuildContext? context, {
  Rect? viewBox,
  ColorFilter? colorFilterOverride,
  Color? color,
  BlendMode? colorBlendMode,
  PictureErrorListener? onError,
}) {
  final PictureConfiguration config = createLocalPictureConfiguration(
    context,
    viewBox: viewBox,
    colorFilterOverride: colorFilterOverride,
    color: color,
    colorBlendMode: colorBlendMode,
  );
  final Completer<void> completer = Completer<void>();
  PictureStream? stream;

  void listener(PictureInfo? picture, bool synchronous) {
    completer.complete();
    stream?.removeListener(listener);
  }

  void errorListener(Object exception, StackTrace stackTrace) {
    if (onError != null) {
      onError(exception, stackTrace);
    } else {
      FlutterError.reportError(FlutterErrorDetails(
        context: ErrorDescription('picture failed to precache'),
        library: 'SVG',
        exception: exception,
        stack: stackTrace,
        silent: true,
      ));
    }
    completer.complete();
    stream?.removeListener(listener);
  }

  stream = provider.resolve(config, onError: errorListener)
    ..addListener(listener, onError: errorListener);
  return completer.future;
}