sourceToProvider function

ImageProvider<Object> sourceToProvider(
  1. String source
)

Converts common canvas image refs into Flutter ImageProviders.

App-specific refs such as media:... should be resolved by the host before reaching this helper.

Implementation

ImageProvider<Object> sourceToProvider(String source) {
  final raw = source.trim();
  final lower = raw.toLowerCase();

  // Standard inline image data. This must never fall through to
  // fileImageProvider on web.
  if (lower.startsWith('data:')) {
    final data = UriData.parse(raw);
    final bytes = data.contentAsBytes();
    return MemoryImage(Uint8List.fromList(bytes));
  }

  final ref = parseCanvasAssetRef(raw);

  if (ref is CanvasUrlAssetRef || ref is CanvasBlobUrlAssetRef) {
    return NetworkImage(ref.raw);
  }

  if (ref is CanvasDataUriAssetRef) {
    final data = UriData.parse(ref.raw);
    final bytes = data.contentAsBytes();
    return MemoryImage(Uint8List.fromList(bytes));
  }

  if (ref is CanvasSchemeAssetRef && ref.scheme == 'asset') {
    return AssetImage(ref.payload);
  }

  if (ref is CanvasFileAssetRef) {
    return fileImageProvider(ref.path);
  }

  if (ref is CanvasRawAssetRef && _looksLikeFlutterAssetPath(ref.raw)) {
    return AssetImage(ref.raw);
  }

  return fileImageProvider(ref.raw);
}