Home > Back-end >  Unable to load Image asset exception thrown in Flutter
Unable to load Image asset exception thrown in Flutter

Time:09-28

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

CodePudding user response:

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/

CodePudding user response:

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'),

CodePudding user response:

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\

CodePudding user response:

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.

CodePudding user response:

Add asset path in pubspec.yaml as

assets:
  - images/diamond.png

or you can also add parent folder path

assets:
  - images/
  • Related