Flutter Image无法加载的原因之一和解决办法
最近学习Flutter时发现Image无法加载本地图片,解决办法如下:
修改publicspec.yaml文件的assets字段,注意的是assets前面有空格,至少要和#对其。
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
assets:
- images/1.png
- assets/images/2.png
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
第二,在项目的根目录下面创建assets文件夹,再创建images子文件夹,但是在程序里面的路径无需加assets,直接写images下面的图片名称即可。要注意在images目录下添加2.0X和3.0X两个文件夹。
@override
List<Widget> _getListdata() {
var tempList = listData.map((Value) {
return Container(
child: Column(
children: <Widget>[
//Image.network(Value['imageUrl']),
Image.asset("images/1.png"),
Text(Value['title']),
],
),
decoration: BoxDecoration(
border: Border.all(
color: Color(23),
)),
);
});
return tempList.toList();
}