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
Here's my pubspec.yaml file:
I assume that your image is under the
assets
folder. So, you need to call image like this:And be sure that you wrote this correctly in your
pubspec.yaml
:make changes in your
pubspec.yaml
fileadd this line to import all the files present in
images
folderfor showing image you can use any one of the code shown below
or
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.
Add asset path in pubspec.yaml as
or you can also add parent folder path