toImageSync method Null safety

Image toImageSync(
  1. int width,
  2. int height
)

Synchronously creates a handle to an image from this scene.

The returned image will be width pixels wide and height pixels high. The picture is rasterized within the 0 (left), 0 (top), width (right), height (bottom) bounds. Content outside these bounds is clipped.

The image object is created and returned synchronously, but is rasterized asynchronously. If the rasterization fails, an exception will be thrown when the image is drawn to a Canvas.

If a GPU context is available, this image will be created as GPU resident and not copied back to the host. This means the image will be more efficient to draw.

If no GPU context is available, the image will be rasterized on the CPU.

Implementation

Image toImageSync(int width, int height) {
  if (width <= 0 || height <= 0) {
    throw Exception('Invalid image dimensions.');
  }

  final _Image image = _Image._();
  final String? result =  _toImageSync(width, height, image);
  if (result != null) {
    throw PictureRasterizationException._(result);
  }
  return Image._(image, image.width, image.height);
}