toImagePng method

Future<Uint8List?> toImagePng({
  1. double pixelRatio = 1.0,
  2. FutureOr<void> onError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
})

Implementation

Future<Uint8List?> toImagePng({
  double pixelRatio = 1.0,
  FutureOr<void> Function(Object error, StackTrace stackTrace)? onError,
}) async {
  try {
    final RenderRepaintBoundary boundary = findRenderObject() as RenderRepaintBoundary;

    final ui.Image image = await boundary.toImage(
      pixelRatio: pixelRatio,
    );

    final ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    if (byteData == null) {
      return null;
    }

    return byteData.buffer.asUint8List();
  } catch (e, stack) {
    if (onError != null) {
      await onError(e, stack);
    }
    return null;
  }
}