measureString method Null safety

Size measureString(
  1. String text,
  2. {Size? layoutArea,
  3. PdfStringFormat? format}

Measures a string by using this font.

//Create a new PDF document.
PdfDocument document = PdfDocument();
//Add a page to the document.
PdfPage page = document.pages.add();
//Create PDF graphics for the page.
PdfGraphics graphics =;
//Create a new PDF font instance.
PdfFont font = PdfStandardFont(PdfFontFamily.helvetica, 12);
String text = "Hello World!";
//Measure the text.
Size size = font.measureString(text);
//Draw string to PDF page.
graphics.drawString(text, font,
    bounds: Rect.fromLTWH(0, 0, size.width, size.height));
//Saves the document.
List<int> bytes =;
//Dispose the document.


Size measureString(String text, {Size? layoutArea, PdfStringFormat? format}) {
  layoutArea ??= const Size(0, 0);
  final _PdfStringLayouter layouter = _PdfStringLayouter();
  final _PdfStringLayoutResult result = layouter._layout(text, this, format,
      width: layoutArea.width, height: layoutArea.height);
  return result._size.size;