getImageProvider method

ImageProvider<Object>? getImageProvider(
  1. dynamic value,
  2. Map? spec
)

Implementation

ImageProvider? getImageProvider(dynamic value, Map? spec) {
  if (value == null) {
    return null;
  }

  if (spec == null || spec.isEmpty) {
    // try to infer provider type
    if (value is Uint8List) {
      spec = {"_type": "MemoryImage"};
    } else if (value is String) {
      if (value.startsWith("assets/")) {
        spec = {"_type": "AssetImage"};
      } else if (value.startsWith("http")) {
        spec = {"_type": "NetworkImage"};
      } else {
        spec = {"_type": "FileImage"};
      }
    } else {
      return null;
    }
  }

  switch (spec["_type"]) {
    case "AssetImage":
      return AssetImage(value, package: spec["package"]);
    case "NetworkImage":
      return NetworkImage(value,
          scale: parseDouble(spec["scale"], defaultValue: 1));
    case "MemoryImage":
      return MemoryImage(value,
          scale: parseDouble(spec["scale"], defaultValue: 1));
    case "FileImage":
      return FileImage(File(value),
          scale: parseDouble(spec["scale"], defaultValue: 1));
    default:
      return null;
  }
}