fetchGif function

Future<List<ImageInfo>> fetchGif(
  1. ImageProvider<Object> provider
)

Implementation

Future<List<ImageInfo>> fetchGif(ImageProvider provider) async {
  List<ImageInfo>? infos;
  dynamic data;
  String key = provider is NetworkImage
      ? provider.url
      : provider is AssetImage
          ? provider.assetName
          : provider is MemoryImage
              ? provider.bytes.toString()
              : "";
  if (GifImage.cache.caches.containsKey(key)) {
    infos = GifImage.cache.caches[key];
    return infos ?? [];
  }
  if (provider is NetworkImage) {
    final Uri resolved = Uri.base.resolve(provider.url);
    final HttpClientRequest request = await _httpClient.getUrl(resolved);
    provider.headers?.forEach((String name, String value) {
      request.headers.add(name, value);
    });
    final HttpClientResponse response = await request.close();
    data = await consolidateHttpClientResponseBytes(
      response,
    );
  } else if (provider is AssetImage) {
    AssetBundleImageKey key = await provider.obtainKey(ImageConfiguration());
    data = await key.bundle.load(key.name);
  } else if (provider is FileImage) {
    data = await provider.file.readAsBytes();
  } else if (provider is MemoryImage) {
    data = provider.bytes;
  }

  ui.Codec? codec = await PaintingBinding.instance.instantiateImageCodecWithSize(data.buffer.asUint8List());
  infos = [];
  for (int i = 0; i < (codec.frameCount); i++) {
    FrameInfo frameInfo = await codec.getNextFrame();
    infos.add(ImageInfo(image: frameInfo.image));
  }
  GifImage.cache.caches.putIfAbsent(key, () => infos!);
  return infos;
}