【发布时间】:2019-03-07 22:48:39
【问题描述】:
我想要做的是在 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...);
}
}
所以,由于我使用的是无状态小部件,所以我必须事先知道图像是否存在,否则我会加载 null 对吗?
【问题讨论】: