skip to Main Content

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)
====================================================================================================

5

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. 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/
    
    Login or Signup to reply.
  3. 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'),
    
    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
  5. Add asset path in pubspec.yaml as

    assets:
      - images/diamond.png
    

    or you can also add parent folder path

    assets:
      - images/
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search