我想做的是在 Material Widget 中加载一张图片,用于 ListTile 中,但这个资源可能不存在。
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...);
}
}
所以,由于我正在使用无状态小部件,我必须事先知道图像是否存在,否则我会加载空值,对吗?