imageProviderOf function

ImageProvider<Object>? imageProviderOf(
  1. dynamic source, {
  2. bool useHttpGetForWeb = true,
  3. void errorCallback()?,
})

Loads based on a URL, file, etc - if the url starts with assets:// then the image is loaded from assets. Also, if the image is from the sunny api server, it will attach the auth header if it's present.

It's usually easier to use [

Implementation

ImageProvider? imageProviderOf(final dynamic source,
    {bool useHttpGetForWeb = true, void errorCallback()?}) {
  if (source == null) return null;
  if (source is File) return FileImage(source);
  if (source is PlatformFile)
    return StreamImageProvider.ofPFile(PFile.of(source)!);
  if (source is PFile) return StreamImageProvider.ofPFile(source);
  if (source is Uint8List) return MemoryImage(source);
  if (source is ChosenMedia) {
    if (source.content != null) {
      return imageProviderOf(
          (source.content as IImageContent?)?.imageUrl?.toUri());
    } else if (source.file != null) {
      return StreamImageProvider.ofPFile(source.file!);
    }
  }

  String imageUrl;
  if (source is Uri) {
    imageUrl = "$source";
  } else if (source is IconInfo) {
    if (source.type == IconType.named_) return null;
    imageUrl = source.icon;
  } else if (source is String) {
    imageUrl = source;
  } else {
    _log.info(
        "IMAGE: WARN ->  UNKNOWN SOURCE $source is ${source.runtimeType}");
    imageUrl = "$source";
  }
  if (imageUrl.startsWith("assets://")) {
    return AssetImage(
      "assets/${imageUrl.replaceFirst("assets://", "")}",
    );
  } else {
//    final apiClient = BearerApiClient.instance;
//    final headers = {
//      if (imageUrl.startsWith(apiClient.config.sunnyBaseUrl))
//        "Authorization": apiClient.authHeader,
//    };
    return CachedNetworkImageProvider(imageUrl,
        imageRenderMethodForWeb: useHttpGetForWeb
            ? ImageRenderMethodForWeb.HttpGet
            : ImageRenderMethodForWeb.HttpGet,
//        headers: headers,
        // ignore: deprecated_member_use
        errorListener: errorCallback);
  }
}