我正在跟踪一个教程,因为我不得不向应用程序中添加图像文件,我像示例中那样进行操作,但是当我运行该应用程序时,没有任何显示。
我尝试更改和复制路径,但是每次都得到相同的结果。 我按照说明更改了pubspec文件。
这是代码的一部分:
Card(
child: Column(
children: <Widget>[
Image.asset('assets/images/download.jpg'),
Text('Food Paradise')
],
这是来自pubspec:
flutter:
uses-material-design: true
assets:
- assets/images/download.jpg
这是来自运行日志:
I/flutter (19491): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE
╞════════════════════════════════════════════════════
I/flutter (19491): The following assertion was thrown resolving an image
codec:
I/flutter (19491): Unable to load asset: assets/images/download.jpg
I/flutter (19491):
I/flutter (19491): When the exception was thrown, this was the stack:
I/flutter (19491): #0 PlatformAssetBundle.load
(package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter (19491): <asynchronous suspension>
I/flutter (19491): #1 AssetBundleImageProvider._loadAsync
(package:flutter/src/painting/image_provider.dart:429:44)
I/flutter (19491): <asynchronous suspension>
I/flutter (19491): #2 AssetBundleImageProvider.load
(package:flutter/src/painting/image_provider.dart:414:14)
I/flutter (19491): #3 ImageProvider.resolve.<anonymous closure>.
<anonymous closure>
(package:flutter/src/painting/image_provider.dart:267:86)
I/flutter (19491): #4 ImageCache.putIfAbsent
(package:flutter/src/painting/image_cache.dart:143:20)
I/flutter (19491): #5 ImageProvider.resolve.<anonymous closure>
(package:flutter/src/painting/image_provider.dart:267:63)
I/flutter (19491): (elided 8 frames from package dart:async)
I/flutter (19491):
I/flutter (19491): Image provider: AssetImage(bundle: null, name:
"assets/images/download.jpg")
I/flutter (19491): Image key: AssetBundleImageKey(bundle:
PlatformAssetBundle#49af1(), name:
I/flutter (19491): "assets/images/download.jpg", scale: 1.0)
该应用正在运行,但没有图像显示。
答案 0 :(得分:0)
首先,看来pubspec.yaml
文件的内容不正确。
应该是
flutter:
uses-material-design: true
assets:
- assets/images/jpgs/test.jpg
此外,检查文件是否位于与pubspec.yaml
相对的正确路径 。
最后,运行flutter packages get
更新新文件。
答案 1 :(得分:0)
我的代码中发生了类似的问题,但是我所做的是为从资产文件夹中分离出的图像创建了另一个文件夹,并移动了所有图像,然后在pubspec.yaml文件中运行了该图像
*颤动:
Uses-material-design:true
资产: -images / waiting.png *
在我的代码的一部分中,我使用了这个:
Image.asset('images / waiting.png')
答案 2 :(得分:0)
请记住,使用资产时必须包含目录。 Image.asset("assets/images/apersonreading.jpg"),
答案 3 :(得分:0)
如果您认为您输入的所有代码都正确,那么只需重新运行代码,而不是热重载或热重启。它对我有用。