toBlob method

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

Implementation

Future<Uint8List> toBlob({double? devicePixelRatio, BindingOpItem? currentProfileOp}) {
  flushLayout();

  forceToRepaintBoundary = true;

  Completer<Uint8List> completer = Completer();
  SchedulerBinding.instance.addPostFrameCallback((_) async {
    Uint8List captured;
    RenderBoxModel? _renderBoxModel = renderBoxModel;

    if (_renderBoxModel == null || _renderBoxModel.hasSize && _renderBoxModel.size.isEmpty) {
      // Return a blob with zero length.
      captured = Uint8List(0);
    } else {
      Image image = await _renderBoxModel.toImage(
          pixelRatio: devicePixelRatio ?? ownerDocument.controller.ownerFlutterView.devicePixelRatio);
      ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
      captured = byteData!.buffer.asUint8List();
    }

    completer.complete(captured);
    forceToRepaintBoundary = false;
    // May be disposed before this callback.
    flushLayout();
  });
  SchedulerBinding.instance.scheduleFrame();

  return completer.future;
}