saveAsUint8List static method

Future<Uint8List?> saveAsUint8List(
  1. ImageQuality imageQuality
)

Implementation

static Future<Uint8List?> saveAsUint8List(ImageQuality imageQuality) async {
  try {
    Uint8List? pngBytes;
    double pixelRatio = 1;
    if (imageQuality == ImageQuality.high) {
      pixelRatio = 2;
    } else if (imageQuality == ImageQuality.low) {
      pixelRatio = 0.5;
    }
    // delayed by few seconds because it takes some time to update the state by RenderRepaintBoundary
    await Future.delayed(const Duration(milliseconds: 700))
        .then((value) async {
      RenderRepaintBoundary boundary = stickGlobalKey.currentContext
          ?.findRenderObject() as RenderRepaintBoundary;
      ui.Image image = await boundary.toImage(pixelRatio: pixelRatio);
      ByteData? byteData =
          await image.toByteData(format: ui.ImageByteFormat.png);
      pngBytes = byteData?.buffer.asUint8List();
    });
    // returns Uint8List
    return pngBytes;
  } catch (e) {
    rethrow;
  }
}