如何将我的 QR 生成图像保存到图库中?使用颤振

how to save my QR generated image into gallery? using Flutter

我正在尝试使用 RenderRepaintBoundary 生成图像并想将图像保存在我的本地目录中。
我可以保存图像,但我得到的是黑色图像而不是二维码图像。

我将图像写入目录的代码块:

try {
    RenderRepaintBoundary boundary =  
        globalKey.currentContext.findRenderObject();  
    var image = await boundary.toImage();
    
    ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
    
    Uint8List pngBytes = byteData.buffer.asUint8List();
         
    final file =
              await new File('/<localpath>/image.png').create();
          
    await file.writeAsBytes(pngBytes);
} catch (e) {
    print(e);    
}
    

正在生成二维码的代码块:

RepaintBoundary( key: globalKey,child: QrImage(data: _dataString,size: 0.3 * bodyHeight,), );

这是透明背景造成的; 简单的解决方案是用具有白色背景的容器包装您的 QrImage:

Container(
    color: Colors.white,
    child: QrImage(....
)