resolveImage function

Future<Image> resolveImage(
  1. String href
)

Resolves an image reference, potentially downloading it via HTTP.

Implementation

Future<Image> resolveImage(String href) async {
  assert(href != '');

  final Future<Image> Function(Uint8List) decodeImage =
      (Uint8List bytes) async {
    final Codec codec = await instantiateImageCodec(bytes);
    final FrameInfo frame = await codec.getNextFrame();
    return frame.image;
  };

  if (href.startsWith('http')) {
    final Uint8List bytes = await httpGet(href);
    return decodeImage(bytes);
  }

  if (href.startsWith('data:')) {
    final int commaLocation = href.indexOf(',') + 1;
    final Uint8List bytes = base64.decode(
        href.substring(commaLocation).replaceAll(_whitespacePattern, ''));
    return decodeImage(bytes);
  }

  throw UnsupportedError('Could not resolve image href: $href');
}