toBlob method

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

Implementation

Future<Uint8List> toBlob({double? devicePixelRatio}) {
  forceToRepaintBoundary = true;

  Completer<Uint8List> completer = Completer();
  SchedulerBinding.instance.addPostFrameCallback((_) async {
    if (!renderStyle.isRepaintBoundary()) {
      String msg = 'toImage: the element is not repaintBoundary.';
      completer.completeError(Exception(msg));
      return;
    }

    if (!isRendererAttached) {
      String msg = 'toImage: the element is not attached to document tree.';
      completer.completeError(Exception(msg));
      return;
    }

    Uint8List captured;
    // RenderBoxModel? _renderBoxModel = renderBoxModel;

    if (!renderStyle.hasRenderBox() ||
        renderStyle.isBoxModelHaveSize() && renderStyle.boxSize()!.isEmpty) {
      // Return a blob with zero length.
      captured = Uint8List(0);
    } else {
      if (this is HTMLElement) {
        Image image = await ownerDocument.viewport!.toImage(
            devicePixelRatio ??
                ownerDocument.controller.ownerFlutterView!.devicePixelRatio);
        ByteData? byteData =
            await image.toByteData(format: ImageByteFormat.png);
        captured = byteData!.buffer.asUint8List();
      } else {
        Image image = await renderStyle.toImage(devicePixelRatio ??
            ownerDocument.controller.ownerFlutterView!.devicePixelRatio);
        ByteData? byteData =
            await image.toByteData(format: ImageByteFormat.png);
        captured = byteData!.buffer.asUint8List();
      }
    }

    completer.complete(captured);
    forceToRepaintBoundary = false;
  });
  SchedulerBinding.instance.scheduleFrame();

  return completer.future;
}