getBytes method

Future<MapEntry<Uint8List?, Size>> getBytes({
  1. ImageByteFormat format = ImageByteFormat.rawRgba,
})

Resolves the ImageProvider´s bytes and returns the bytes together with their Size.

Implementation

Future<MapEntry<Uint8List?, Size>> getBytes({
  ImageByteFormat format = ImageByteFormat.rawRgba,
}) async {
  Size? size;
  final imageStream = resolve(ImageConfiguration.empty);
  final Completer<Uint8List?> completer = Completer<Uint8List?>();
  final ImageStreamListener listener = ImageStreamListener(
    (imageInfo, synchronousCall) async {
      size = Size(
        imageInfo.image.width.toDouble(),
        imageInfo.image.height.toDouble(),
      );

      final bytes = await imageInfo.image.toByteData(format: format);

      if (!completer.isCompleted) {
        completer.complete(bytes?.buffer.asUint8List());
      }
    },
  );

  imageStream.addListener(listener);
  final imageBytes = await completer.future;
  imageStream.removeListener(listener);

  return MapEntry(imageBytes, size!);
}