11
votes

flutter can not find my images assets, what can I be doing wrong?

I got the error on debugger:

Launching lib/main.dart on XT1097 in debug mode... Built build/app/outputs/apk/debug/app-debug.apk.

I/flutter (  876): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞══
I/flutter (  876): Unable to load asset: assets/images/user/background.jpg
I/flutter (  876): "assets/images/user/background.jpg", scale: 1.0)

My pubspec.yml:

environment:
    sdk: ">=2.0.0-dev.68.0 <3.0.0"

dependencies:
    sqflite: any
    path_provider: '>=0.3.0'
    charts_flutter: any
    cupertino_icons: ^0.1.2
    material_search: ^0.2.8
    dio: ^1.0.3
    flutter:
        sdk: flutter

dev_dependencies:
    flutter_test:
        sdk: flutter


flutter:
    uses-material-design: true
    assets:
        - assets/images/

and the code:

                new UserAccountsDrawerHeader(
                    decoration : BoxDecoration(
                        image: new DecorationImage(
                            image: AssetImage('assets/images/user/background.jpg'),
                            fit: BoxFit.cover
                        ),
                        color: Colors.blue,
                    ),
                    accountName: new Text('Diego Botelho'),
                    accountEmail: new Text('[email protected]'),
                    currentAccountPicture: new GestureDetector(
                        onTap: () => print('Toque na imagem'),
                        child: new CircleAvatar(
                            backgroundImage: AssetImage('assets/images/user/avatar.png'),
                        )
                    )
                ),
12

12 Answers

21
votes

As far as I know currently only files directly placed under the registered directory are added to assets.

This should work:

flutter:
  uses-material-design: true
  assets:
   - assets/images/
   - assets/images/user/
3
votes

This is how this resolved for me.

Previously I was using the exact paths to images as suggested

 assets:
 - assets/logo_linked_in.jpg
 - assets/logo_medium.png
 - assets/logo_stackoverflow.png
 - assets/myself.jpg
 - assets/sparta.jpg
 - assets/fox.png

Then I tried just the parent directory name, which was just using:

assets:
 - assets/

Voila! It worked like a charm!

I hope this helps someone.

P.S. - For newbies like me:

Make sure you have correctly indented the pubspec.yaml as this is very important for the build.

3
votes

In my case I have to correct the alignments between my assets and the uses-material-design lines in pubspec.yaml.

flutter:
    uses-material-desgin: true
    assets:
        - images/3.jpg
1
votes

I had the same error. In my case it was because the emulator of the phone that I am using does not have access to the internet.

Run the application on a physical mobile device and it worked immediately.

Solution connect the emulator to the internet.

1
votes

Uninstall app on device/emulator Run flutter clean. Run app

0
votes

In my case I have two space between _ and assets that's why this exception coming

-  assets/images/logo.png
-  assets/images/bg.jpg

Now I fixed by

assets:
- assets/images/logo.png
- assets/images/bg.jpg
0
votes

In my case, while i was using relative path like that Image.asset('./../images/welcome.png'), it doesn't work.

But if i use absolute path like that Image.asset('images/welcome.png'), it works !

0
votes

Previously I set images paths as following

"assets/images/SU.jpg"

then I had this error so I changed it as following

"images/SU.jpg"

then it worked!!

0
votes

I faced this error when i was accidentally passing number parameter as URL to get image from server.

leading: CircleAvatar(
        backgroundImage: NetworkImage(imageUrl),
      ),

//imageUrl should be correct image url not other type.

0
votes

Im my case I was in hot reload mode and nothing was happening. I had to click hot restart for the image to show up.

Hot Restart

0
votes

if web app not loading images from Image.network(url), then do following in the terminal:

  1. flutter config --enable-web // now restart ur editor
  2. flutter build web
  3. flutter create .
  4. flutter run

from https://flutter.dev/docs/get-started/web

0
votes

I was facing the same error with correct indentation and the error was in the YMAL file by default has this line publish_to: 'none' after removing that line. It started to output the images correctly read more: The pubspec file

name: ugandaclearfamilywillsuganda description: A New way of Creating your legal Wishes after death version: 1.0.0+1 environment: sdk: ">=2.12.0 <3.0.0" dependencies: flutter: sdk: flutter dev_dependencies: flutter_test: sdk: flutter flutter: uses-material-design: true assets: - images/