problem in importing images in flutter using image.asset()

750 views Asked by At

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/
3

There are 3 answers

0
Ishteyaque Ahmad On

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/
4
Dani3le_ On

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.

0
plotsklapps On

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/