【发布时间】:2026-01-06 15:50:01
【问题描述】:
我正在尝试从设备内存中加载图像employerPic。这张照片可能不存在,所以它应该回退到头像。 Logcat 抛出这个错误:
未处理的异常:FileSystemException:无法检索长度 文件,路径= '/data/user/0/uk.co.neighbourly/app_flutter/MYjFq4FFKKiayOl03sdX.jpg' (操作系统错误:没有这样的文件或目录,errno = 2)
基本上,我已经给了颤振一个文件路径,但是该文件在这个实例中不存在。我已将整个尝试包含在 try{} 块中,因此我可以退回到异常。你可以看到我使用了 2 个例外,一个 on FileSystemException 和一个通用的 catch(error){},但是当 try 子句失败时,都不会被触发。什么!
我想要一个解决方案,或更优雅的方式来将图像回退到有保证的图像。
if(_appDocsDir != null) {
File employerPic = _getEmployerPicFile();
if(employerPic != null) {
print('JOB TILE: GOT IMAGE FROM DEVICE! path: ${employerPic.path}');
try {
profilePic = ClipOval(
child: Image.file(
employerPic, width: 60, height: 60, fit: BoxFit.cover),
);
} on FileSystemException {
print(LOG + 'failed to load profilePic from mem, attempting Avatar');
profilePic = Image.asset(
PersonUtils.getAvatarFromId[widget._job.avatar], fit: BoxFit.cover);
} catch(error) {
print(LOG + '!Strange! error when attempting to load employerPhoto: $error');
};
}
}
【问题讨论】:
标签: image file flutter exception