Reputation: 51
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,
),
],
),
);
Upvotes: 5
Views: 24885
Reputation: 128
After added new assets, it's preferable to stop the current debug session and run again.
Upvotes: 0
Reputation: 374
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
Upvotes: 2
Reputation: 2627
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
Upvotes: 1
Reputation: 11
Stop your emulator and Project and Type :flutter clean in terminal and run again
Upvotes: 0
Reputation: 374
on your terminal :
flutter pub get
to refresh pubspec.yaml
Upvotes: 0
Reputation: 16
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
Upvotes: 0
Reputation: 819
Have you added the asset in your pubspec.yaml file?
assets:
- assets/images/rose.jpg
Upvotes: 0