Search code examples
imageflutterassets

Flutter app does not show the image - Unable to load asset


I'm following a tutorial in flutter where I had to add an image file to the app, I did it like the example but when I run the app nothing shows.

I tried to change and copy the path but the same result every time. I change the pubspec.file as explained.

This is part of the code:

          Card(
            child: Column(
              children: <Widget>[
                Image.asset('assets/images/download.jpg'),
                Text('Food Paradise')
              ],

This is from the pubspec:

flutter:
  uses-material-design: true
assets:
    - assets/images/download.jpg

This is from the RUN log:

  I/flutter (19491): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE 
╞════════════════════════════════════════════════════
I/flutter (19491): The following assertion was thrown resolving an image 
codec:
I/flutter (19491): Unable to load asset: assets/images/download.jpg
I/flutter (19491): 
I/flutter (19491): When the exception was thrown, this was the stack:
I/flutter (19491): #0      PlatformAssetBundle.load 
(package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter (19491): <asynchronous suspension>
I/flutter (19491): #1      AssetBundleImageProvider._loadAsync 
(package:flutter/src/painting/image_provider.dart:429:44)
I/flutter (19491): <asynchronous suspension>
I/flutter (19491): #2      AssetBundleImageProvider.load 
(package:flutter/src/painting/image_provider.dart:414:14)
I/flutter (19491): #3      ImageProvider.resolve.<anonymous closure>. 
<anonymous closure> 
(package:flutter/src/painting/image_provider.dart:267:86)
I/flutter (19491): #4      ImageCache.putIfAbsent 
(package:flutter/src/painting/image_cache.dart:143:20)
I/flutter (19491): #5      ImageProvider.resolve.<anonymous closure> 
(package:flutter/src/painting/image_provider.dart:267:63)
I/flutter (19491): (elided 8 frames from package dart:async)
I/flutter (19491): 
I/flutter (19491): Image provider: AssetImage(bundle: null, name: 
"assets/images/download.jpg")
I/flutter (19491): Image key: AssetBundleImageKey(bundle: 
PlatformAssetBundle#49af1(), name:
I/flutter (19491): "assets/images/download.jpg", scale: 1.0)

The app is running but no image shows.


Solution

  • Firstly it looks like the pubspec.yaml file content is not properly indented.

    It should be something like

    flutter:
      uses-material-design: true
    
      assets:
        - assets/images/jpgs/test.jpg
    

    Furthermore, check if your file is at the correct path, which is relative to pubspec.yaml.
    Lastly, run flutter packages get to update the new file. then run flutter clean then flutter run