Flutter asset image displays the wrong image

Viewed 320

I have am images folder and inside it i have table.png and then i have a main_menu folder in which i have another table.png file.

  • images
    • main_menu
      • table.png
      • //...other images here
    • table.png
    • //...other images here

When I do

                      decoration: BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage("images/table.png"),
                        )
                      ),

it takes the table.png image from the images/main_menu/ folder instead ... why is that?

Don't tell me it searches for the first place it finds the table.png file and just displays that. And in this case table.png under main_menu is the first one it finds and that's why it displays it. It's just a hunch but ... if it's true, then this is a bug, no ?

Cheers.

3 Answers

Try with this :

  decoration: BoxDecoration(
    image: DecorationImage(
      image: AssetImage("./images/table.png"), 

     //instead of AssetImage("images/table.png") 

    )
  ),

There are a different way to define images but i prefer this if picture in sub folder . This is my folder of assets->images I attaching Screenshot of folder below

assets-->images

And in pubspec.yaml i define like this

pubspec.yaml file

hope it will works :D

And in class use like this

title: Image(
      image: AssetImage('assets/images/xyz.png'),
    ),

Reference this part: Flutter Docs: Specifying assets

Note: Only files located directly in the directory are included unless there are files with the same name inside a subdirectory (see Asset Variants). To add files located in subdirectories, create an entry per directory.

Related