扑.加载前检查文件是否存在 [英] Flutter. Check if a file exists before loading it
本文介绍了扑.加载前检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要做的是将图像加载到材料小部件中以在ListTile中使用它,但是该资产可能不存在.
What I want to do is load an image in a Material Widget to use it in a ListTile, but this asset might not exist.
class MyImage extends StatelessWidget {
final imagePath;
MyIcon(String iconName) {
try { // check if imagePath exists. Here is the problem
imagePath = check('assets/$iconName.png/');
} catch (e, s) { // if not
imagePath = 'assets/$iconName.png/';
}
}
@override
Widget build(BuildContext context) {
return Material(...here I will load the imagePath...);
}
}
因此,由于我使用的是无状态小部件,因此我必须事先知道该图像是否存在,否则我会加载null权吗?
So, since I'm using a Stateless widget, I have to know beforehand if the image exists, otherwise I'll load a null right?
我对Futter还是很陌生,所以我不知道这是否是一个显而易见的问题
I'm pretty new to Futter so I don't know if this is an obvious question
谢谢!
推荐答案
为了查看应用使用的内部本地存储中是否存在文件:
In order to see whether or not a file exists in internal local storage of the app use:
import 'dart:io' as io;
// for a file
io.File(path).exists();
// for a directory
io.Directory(path).exists();
这篇关于扑.加载前检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文