1. 程式人生 > 其它 >Flutter Image無法載入的原因之一和解決辦法

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();
}