I cant Upload image as a background to a stack, i have added the image in assets folder and added it to pubspec.yaml and showing me that error:
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)
Code:
return new Scaffold(
backgroundColor: Colors.greenAccent,
body: new Stack(
children: [
new Image(
image: new AssetImage("assets/images/rose.jpg"),
fit: BoxFit.cover,
),
],
),
);
Run flutter clean
and run again.
Your assets folder is in the right place which is in the root of your project folder.
The error is because you're using the image Provider AssetImage instead of Image.asset("assets/images/rose.jpg").
After making changes to your image asset, make sure you save it to automatically retrieve the new image dependency or run $ flutter pub get.
Also check that you've added the image path to the assets subsection in your pubspec.yaml like this:
flutter:
assets:
- assets/images/rose.jpg
Have you added the asset in your pubspec.yaml file?
assets:
- assets/images/rose.jpg
Please check that your pubspec.yaml file is correctly indented as below. Incorrect spacing/indentation may result in the image not being found.
flutter: assets: - assets/images/rose.jpg
on your terminal:
flutter pub get
to refresh pubspec.yaml
Stop your emulator and Project and Type: flutter clean in terminal and run again
Image.asset("images/abc.jpg"),
instead of this:
Image.asset("assets/images/abc.jpg"),
PLUS:
Make sure it's written in pubspec.yaml as
assets:
- assets/images/ //ONLY
And update pubspec if you added a recent Image with
flutter pub get
After added new assets, it's preferable to stop the current debug session and run again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.