我正在呼叫Image.asset()然后呼叫在我的資產檔案夾中保存路徑的變數。這是我的代碼,這里的代碼是將路徑分配到 imagePath 變數中。我在控制臺中列印它并且變數是正確的
Future getName(String publisherUid) async {
FirebaseFirestore.instance
.collection('users')
.doc(publisherUid)
.get()
.then((value) {
setState(() {
publisherSchool = value.get('school');
publisherFirstName = value.get('first-name');
publisherLastName = value.get('last-name');
publisherFullName = publisherFirstName ' ' publisherLastName;
publisherUserIcon = value.get('userIcon');
imagePath = 'assets/images/$publisherUserIcon';
//print('$imagePath');
});
});
}
然后就在這里在串列磁貼中顯示它
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 5),
ListTile(
leading: Image.asset(imagePath),
title: Text(publisherFullName,
style: TextStyle(fontSize: 14)),
然后它有一個錯誤說“FlutterError(無法加載資產:)”。我嘗試將變數顯示為這樣的文本Text(imagePath),它顯示正確,請參閱影像以供參考
然后我嘗試像這樣手動顯示資產
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 5),
ListTile(
leading: Image.asset('assets/images/001-panda.png'),
title: Text(publisherFullName,
style: TextStyle(fontSize: 14)),
當您手動撰寫影像路徑時,我沒有收到任何錯誤這是輸出 
順便說一句,這是我在 pubspec.yaml 中的代碼。它有適當的縮進,因為如果 pubspec.yaml 中的資產錯誤,我將無法手動呼叫路徑
assets:
- assets/images/
uj5u.com熱心網友回復:
您必須擁有與您在文本中列印的名稱完全相同的資產。
在檔案夾“assets/images/”中查看該名稱。檢查空白區域或類似的東西。
uj5u.com熱心網友回復:
您是否正確設定
pubspec.yaml、 和image name should exactly match。您應該
image path作為字串傳遞,如下所示Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox(height: 5), ListTile( leading: Image.asset(imagePath.toString()), title: Text(publisherFullName, style: TextStyle(fontSize: 14)),
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335689.html
標籤:扑
