Flutter-如何捕获FileSystemException



我的flutter应用程序将屏幕截图保存到磁盘(在flutter台式机Mac和Windows上(。如果由于任何原因导致写入失败,则捕获和错误处理不起作用。在";捕获";将永远不会执行,尽管调试输出显示

[错误:flutter/lib/ui/ui_start_state.cc(198(]未处理的异常:FileSystemException:无法打开文件…]

我在这里错过了什么?

实际代码片段:

final dFile = File('$dPath(AS-Logo) $logoText.png');
screenshotController
.captureFromWidget(makeLogoShow(completeImageSize, logoText))
.then((capturedImage) {
try {
dFile.writeAsBytes(capturedImage);
} on FileSystemException catch (e) {
print("Error: $e");
return false;
}
});

所以我解决这个问题的方法是编写一个不同的函数来将小部件转换为Image文件。

File _processWidgetImage() {
String yourFilePath = 'your/file/path.png';
Future<Uint8List> image = screenshotController
.captureFromWidget(makeLogoShow(completeImageSize, logoText));
Future<File> f = _widgetToImageFile(image, yourFilePath);
return await f;
}
Future<File> _widgetToImageFile(Future<Uint8List> capturedImage, String path) async {
Uint8List cp = await capturedImage;
final dFile = await File(path).writeAsBytes(cp);
return dFile;
}

你应该发布你的makeLogoShow方法。那里可能也有一些不正常的东西。该方法需要返回一个可查看的小部件。

最新更新