toBlob method

Future<Uint8List> toBlob({
  1. double? devicePixelRatio,
})
inherited

Implementation

Future<Uint8List> toBlob({ double? devicePixelRatio }) {
  devicePixelRatio ??= window.devicePixelRatio;

  Completer<Uint8List> completer = Completer();
  if (targetId != HTML_ID) {
    convertToRepaintBoundary();
  }
  renderBoxModel!.owner!.flushLayout();

  SchedulerBinding.instance!.addPostFrameCallback((_) async {
    Uint8List captured;
    RenderBoxModel? renderObject = targetId == HTML_ID
        ? elementManager.viewportElement.renderBoxModel
        : renderBoxModel;
    if (renderObject!.hasSize && renderObject.size.isEmpty) {
      // Return a blob with zero length.
      captured = Uint8List(0);
    } else {
      Image image = await renderObject.toImage(pixelRatio: devicePixelRatio!);
      ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
      captured = byteData!.buffer.asUint8List();
    }

    completer.complete(captured);
  });
  SchedulerBinding.instance!.scheduleFrame();

  return completer.future;
}