toUint8List function

Future<Uint8List> toUint8List(
  1. GlobalKey<State<StatefulWidget>> imageKey
)

Implementation

Future<Uint8List> toUint8List(GlobalKey imageKey) async {
  try {
    RenderRepaintBoundary? boundary =
        imageKey.currentContext?.findRenderObject() as RenderRepaintBoundary?;
    if (boundary == null) {
      return Uint8List(0);
    }

    await Future.delayed(Duration.zero);
    final ui.Image image = await boundary.toImage(pixelRatio: 3.0);
    ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    return byteData?.buffer.asUint8List() ?? Uint8List(0);
  } catch (e) {
    debugPrint('Error converting to Uint8List: $e');
    return Uint8List(0);
  }
}