decodeImageData static method

Future<Image?> decodeImageData(
  1. Uint8List data, {
  2. int width = 100,
  3. int height = 100,
})

Decode raw image bytes into a ui.Image asynchronously.

This method decodes the provided data buffer into a ui.Image. The operation is performed asynchronously and the returned Future will complete once decoding finishes. If decoding fails the Future will complete with null.

Parameters

  • data: Raw image bytes to decode (PNG, JPEG or raw pixel data as supported by the underlying platform).
  • width: Desired width for the decoded image. When applicable the decoder may use this to scale or interpret the input. Defaults to 100.
  • height: Desired height for the decoded image. Defaults to 100.

Returns

  • Future<ui.Image?>: Completes with the decoded ui.Image on success, or null when decoding fails.

Implementation

static Future<ui.Image?> decodeImageData(
  final Uint8List data, {
  final int width = 100,
  final int height = 100,
}) async {
  final Completer<ui.Image?> completer = Completer<ui.Image?>();

  ui.decodeImageFromPixels(data, width, height, ui.PixelFormat.rgba8888, (
    final ui.Image img,
  ) async {
    completer.complete(img);
  });

  return completer.future;
}