'problem in importing images in flutter using image.asset()
I'm constantly getting this error saying:
The following assertion was thrown resolving an image codec: Unable to load asset: ../images/mall.png
Even though the YAML file is correct.
This is my pubspec.yaml
environment:
sdk: ">=2.18.0-10.0.dev <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
# The following section is specific to Flutter packages.
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:
- images/
Solution 1:[1]
Your pubspec.yaml should look something like this:
assets:
- images/mall.png
- images/example.png
and not
assets:
- images/
Add the all images you need.
Solution 2:[2]
Create a folder named as assets in PROJECT DIRECTORY not in lib or some other folder. Drag and drop your image into assets folder. Now your pubspec.yaml would be like
assets:
- assets/
Solution 3:[3]
I think you have a path that looks like:
/assets/images/mall.png
but your pubspec.yaml is saying it's:
/images/mall.png
and can't find it.
Make sure your filetree is the same as you're telling it is in pubspec.yaml and don't forget to indent correctly.
To be clear:
assets:
- images/
does NOT mean /assets/images/
it should then be:
assets:
- assets/images/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dani3le_ |
| Solution 2 | Ishteyaque Ahmad |
| Solution 3 | plotsklapps |
