我想制作一个简单的flutter应用程序,并想从我的资产中添加图片,我已经尝试过了,但是该图片仍然无法加载。 这就是我加载图片的方式
Image.asset('assets/images/food.jpg')
pubspec.yaml代码:
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/food.jpg
调试:
I/flutter (18975): The following assertion was thrown resolving an image codec:
I/flutter (18975): Unable to load asset: assets/images/food.jpg
I/flutter (18975): Unable to load asset: assets/images/food.jpg
I/flutter (18975):
I/flutter (18975): When the exception was thrown, this was the stack:
I/flutter (18975): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter (18975): <asynchronous suspension>
I/flutter (18975): #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:433:44)
I/flutter (18975): <asynchronous suspension>
I/flutter (18975): #2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:418:14)
I/flutter (18975): #3 ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:285:105)
I/flutter (18975): #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:157:22)
I/flutter (18975): #5 ImageProvider.resolve.<anonymous closure> (package:flutter/src/painting/image_provider.dart:285:82)
I/flutter (18975): (elided 13 frames from package dart:async)
I/flutter (18975):
I/flutter (18975): Image provider: AssetImage(bundle: null, name: "assets/images/food.jpg")
I/flutter (18975): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#df91c(), name: "assets/images/food.jpg",
I/flutter (18975): scale: 1.0)
这是我的图像文件夹,如下所示:
答案 0 :(得分:1)
您的格式不符合pubspec.yaml中的要求。您必须添加一个标签,因为您可以从代码中看到它。
赞:
flutter:
assets:
- graphics/background.png
有关更多详细信息,请参见此。 https://flutter.dev/docs/development/ui/assets-and-images