2

Here's my code:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
      backgroundColor: Colors.blueGrey,
      appBar: AppBar(
        title: Text('I AM RICH'),
        backgroundColor: Colors.blueGrey[900],
      ),
      body: Center(
        child: Image(
          image: AssetImage('images/diamond.png'),
        ),
      ),
    ),
    ),
  );
}

THIS IS THE EXCEPTION I AM GETTING.

My pubspec.yaml file follows proper indentation.

======= Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/diamond.png

When the exception was thrown, this was the stack: 
#0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:672:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "images/diamond.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#a2375(), name: "images/diamond.png", scale: 1.0)
====================================================================================================

8
  • can you update your pubspec.yaml? Commented Sep 27, 2021 at 13:04
  • can you post your pubspec file source code? again are you sure the file is in the folder images? Commented Sep 27, 2021 at 13:08
  • sometime your pubsyaml ok but still you cant load image then flutter clean and run your project its work for me hopefully its will also work for you Commented Sep 27, 2021 at 13:16
  • well, if everything is fine, then you just need to stop and run your application again. That's it. Commented Sep 27, 2021 at 13:17
  • stopping the application and re-running it did not work Commented Sep 27, 2021 at 13:28

6 Answers 6

1

I assume that your image is under the assets folder. So, you need to call image like this:

AssetImage('assets/images/diamond.png')

And be sure that you wrote this correctly in your pubspec.yaml:

assets:
  - assets/images/
Sign up to request clarification or add additional context in comments.

4 Comments

No. the 'image' folder is in the root project folder.
Then move it under the assets folder.
shall I create a new assets folder and move the images folder into it?
well, you don't need to move your file. images folder in root is okay, but do mention the folder in pubspec.yaml file in the correct way. I mean the indentations and directory name. After editing pubspec.yaml, do not forget to click on Pub get and it should give you exit code 0 in the message section. After that, do not forget to stop and re-run your application.
1

make changes in your pubspec.yaml file

add this line to import all the files present in images folder

assets:
    - images/

for showing image you can use any one of the code shown below

Image(image: AssetImage('images/diamond.png')),

or

Image.asset('images/diamond.png'),

Comments

1

Add asset path in pubspec.yaml as

assets:
  - images/diamond.png

or you can also add parent folder path

assets:
  - images/

Comments

0

Here's my pubspec.yaml file:

name: i_am_rich
description: Show off your wealth.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter


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

Comments

0

Ultimately the problem is with typing mistake, "images" should be "images/" Interesting how pub get executes with an exit code of 0 even after typing error.

Comments

0

I was having the same problem, and stoping and restarting my app worked for me

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.