Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter assets error: EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE

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'),
                        )
                    )
                ),
like image 997
Diego Botelho Avatar asked Oct 04 '18 10:10

Diego Botelho


4 Answers

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/
like image 92
Günter Zöchbauer Avatar answered Oct 08 '22 09:10

Günter Zöchbauer


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.

like image 25
sud007 Avatar answered Oct 08 '22 09:10

sud007


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
like image 3
Eliod Avatar answered Oct 08 '22 07:10

Eliod


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.

like image 1
Natalia Peña Avatar answered Oct 08 '22 07:10

Natalia Peña