loadNetworkTextureSimple static method

Future<GTexture> loadNetworkTextureSimple(
  1. String url, {
  2. int? width,
  3. int? height,
  4. double resolution = 1.0,
  5. String? cacheId,
})

Loads an image from a network URL and returns a GTexture object. The image is loaded asynchronously, and the returned Future is completed with the GTexture object when the image is fully loaded. The width and height parameters can be used to resize the image. The resolution parameter can be used to scale the image. If cacheId is provided, the loaded texture will be added to the texture cache using cacheId as the key. If the cacheId already exists in the cache, the cached texture will be returned instead.

Throws a FlutterError if the image cannot be loaded.

Implementation

static Future<GTexture> loadNetworkTextureSimple(
  String url, {
  int? width,
  int? height,
  double resolution = 1.0,
  String? cacheId,
}) async {
  if (cacheId != null && textureCache.containsKey(cacheId)) {
    return textureCache[cacheId]!;
  }
  Uint8List bytes;
  // try {
  bytes = await httpGet(url);
  final codec = await ui.instantiateImageCodec(
    bytes,
    allowUpscaling: false,
    targetWidth: width,
    targetHeight: height,
  );
  final image = (await codec.getNextFrame()).image;
  final texture = GTexture.fromImage(image, resolution);
  if (cacheId != null) {
    textureCache[cacheId] = texture;
  }
  return texture;
  // } finally {
  //   throw FlutterError('Unable to texture $url.');
  // }
}