简体   繁体   中英

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/

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.

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/

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/

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM