imageProvider static method

ImageProvider<Object>? imageProvider(
  1. DataSource source,
  2. List<Object> key
)

Returns an ImageProvider from the specifed map.

The source key of the specified map is controlling. It must be a string. If its value is one of the keys in imageProviderDecoders, then the relevant decoder is invoked and its return value is used (even if it is null).

Otherwise, if the source key gives an absolute URL (one with a scheme), then a NetworkImage with that URL is returned. Its scale is given by the scale key (double, defaults to 1.0).

Otherwise, if the source key gives a relative URL (i.e. it can be parsed as a URL and has no scheme), an AssetImage with the name given by the source key is returned.

Otherwise, if there is no source key in the map, or if that cannot be parsed as a URL (absolute or relative), null is returned.

Implementation

static ImageProvider? imageProvider(DataSource source, List<Object> key) {
  final String? image = source.v<String>([...key, 'source']);
  if (image == null) {
    return null;
  }
  if (imageProviderDecoders.containsKey(image)) {
    return imageProviderDecoders[image]!(source, key);
  }
  final Uri? imageUrl = Uri.tryParse(image);
  if (imageUrl == null) {
    return null;
  }
  if (!imageUrl.hasScheme) {
    return AssetImage(image);
  }
  return NetworkImage(image, scale: source.v<double>([...key, 'scale']) ?? 1.0);
}