我无法将图像作为背景上传到堆栈,我已将图像添加到资产文件夹中并将其添加到 pubspec.yaml 并向我显示该错误:
Exception caught by image resource service.
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/images/rose.jpg
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:225:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:668:31)
#2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:651:14)
#3 ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:504:13)
...
Image provider: AssetImage(bundle: null, name: "assets/images/rose.jpg")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#dea48(), name: "assets/images/rose.jpg", scale: 1.0)
代码:
return new Scaffold(
backgroundColor: Colors.greenAccent,
body: new Stack(
children: [
new Image(
image: new AssetImage("assets/images/rose.jpg"),
fit: BoxFit.cover,
),
],
),
);
运行
flutter clean
并再次运行。
Image.asset("images/abc.jpg"),
而不是这个:
Image.asset("assets/images/abc.jpg"),
加:
确保它在 pubspec.yaml 中写为
assets:
- assets/images/ //ONLY
如果您添加了最近的图像,请更新 pubspec
flutter pub get
您的资产文件夹位于正确的位置,位于项目文件夹的根目录中。
该错误是因为您使用的是图像提供程序 AssetImage 而不是 Image.asset("assets/images/rose.jpg")。
对图像资源进行更改后,请确保保存它以自动检索新的图像依赖项或运行 $ flutter pub get。
还要检查您是否已将图像路径添加到 pubspec.yaml 中的资产子部分,如下所示:
flutter:
assets:
- assets/images/rose.jpg
您是否已在 pubspec.yaml 文件中添加了该资源?
assets:
- assets/images/rose.jpg
请检查您的 pubspec.yaml 文件是否正确缩进,如下所示。不正确的间距/缩进可能会导致找不到图像。
flutter:
assets:
- assets/images/rose.jpg
在您的终端上:
flutter pub get
刷新 pubspec.yaml
停止模拟器和项目并在终端中输入:flutter clean并再次运行
添加新资源后,最好停止当前调试会话并再次运行。
我也有同样的问题。我做了热重载,效果很好。您也可以尝试关闭模拟器并再次运行项目。
当然,这是在您确定该路径存在并且该路径在 pubspec.yaml 文件中正确设置之后。
祝你好运!