loadImage static method

Future<Image> loadImage(
  1. String path, {
  2. int? targetWidth,
  3. int? targetHeight,
})

Loads an image from local assets.

The path argument is the path to the image file to be loaded. The optional targetWidth and targetHeight arguments can be used to specify the maximum width and height of the loaded image.

Returns a Future that completes with a ui.Image object.

Implementation

static Future<ui.Image> loadImage(
  String path, {
  int? targetWidth,
  int? targetHeight,
}) async {
  final data = await rootBundle.load(path);
  final bytes = Uint8List.view(data.buffer);
  final codec = await ui.instantiateImageCodec(
    bytes,
    allowUpscaling: false,
    targetWidth: targetWidth,
    targetHeight: targetHeight,
  );
  return (await codec.getNextFrame()).image;
}