skip to Main Content

With Visual Studio Code, I can’t use relative path for all assets. I must use the full path to show any asset images. When I’m using a relative path, the image doesn’t appear.

Do you know how to use relative paths in Visual Studio?

pubspec.yaml

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:
  - diolos/assets/images/

When I link an imagem I must add the full path.

Image.asset(
height: 50,
"/Users/ogosselin/Documents/Projets/AppMobile/diolos/assets/images/diolosLogoVioletEntete.png"),

Image doesn’t appear with a relative path, but it works with the full path.

Update : I have the preview but same behavior
The following assertion was thrown resolving an image codec:
Unable to load asset: "diolos/assets/images/diolosLogoBlancEntete.png".
Exception: Asset not found

enter image description here

3

Answers


  1. It looks like diolos is your project name, so the path to the assets folder in the pubspec.yaml file should be:

    assets:
        - assets/images/
    
    Login or Signup to reply.
  2. enter image description here

    open vs code right click on asset and click copy relative path and then paste this in pubspec.yaml file.

    Login or Signup to reply.
  3. First of all your assets folder will look like this:

    project_name
    ├── assets
    │   └── images
    │       └── diolosLogoVioletEntete.png
    ├── android
    ├── ios
    ├── lib
    └── pubspec.yaml
    

    Then in your pubspec.yaml file add assets like this:

    flutter:
      assets:
        - assets/images/
    

    And use it like this:

    Image.asset('assets/images/diolosLogoVioletEntete.png')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search