Flutter image is not showing

3.9k views Asked by At

I want to show some local image and display it on the screen like this:

 Image(image: AssetImage('assets/images/addFood.png'))

But I cannot see the image on the screen and I am getting no error at all.

Here is what I have done:

In my pubspec.yaml I have added my image folder like this:

  assets:
    - lib/assets/images/

The image named "addFood" is inside images folder as you can see here:

enter image description here

I want to display a simple text with image under it, I can see the text on the screen and I am doing it like this:

  Widget build(BuildContext context) {

    if (favoriteMeals.isEmpty)

    return Column(children: <Widget>[
      Text('no favorites',style: Theme.of(context).textTheme.title,),
      Image(image: AssetImage('assets/images/addFood.png'))
    ]);

    else{
     ......
     ......
     ......
   }

All I can see is the text without the image:

enter image description here

solutions that did not help in my case:

AssetImage is not not displaying image in flutter app

Adding assets and images

mage Not Appearing when Using "new Image.asset" inside a Column

How can I fix it and make sure that the image will be displayed on the screen?

3

There are 3 answers

0
Christopher Moore On BEST ANSWER

You need to provide the full path for an asset when accessing it within the code. Nothing is implied when accessing the assets. Your actual path is lib/assets/images/, but you only do assets/images/. Change your code to include this:

Image(image: AssetImage('lib/assets/images/addFood.png'))
0
Subair K On

Update pubspec.yaml file like this

   assets:
     - directory/subdirectory/
2
Ankit Tale On

It should be

flutter:
   assets:
     - assets/images/

Remove lib from pubspec.yaml path. And to get image in code use,

Image.asset('assets/images/lake.jpg')