Unable to load asset: assets/image1 (Flutter) - flutter

I encountered this problem a few days ago and I was not sure how, but I managed to solve it. I still don't have a clue about what actually causes it. Whenever I create a new flutter project and try to add asset images in my app it throws this error:
======== Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/image01
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: "assets/image01")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#abc8a(), name: "assets/image01", scale: 1.0)
====================================================================================================
I surfed for a while to find a solution but I was unable to find one, sometimes I just restart the app and run '''flutter clean''' to fix it but it isn't working this time. I don't know what to do, the pubsec.yaml and and the code seems to be working fine. I've tried creating new projects and rebuilding gradle but it's no good. Please provide descriptive and helpful answers.
Here's the code:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: CircleAvatar(
radius: 100,
backgroundColor: Colors.blue,
child: Image.asset('assets/image01'),
),
),
],
),
),
),
);
}
}
And here's pubsec.yaml
name: mi_card
description: A new Flutter project.
publish_to: 'none'
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:
- assets/

Try below code
Add below code in pubspec.yaml file.
flutter:
assets:
- assets/
uses-material-design: true
and create Image Widget, add your image path that save you in any folder like assets/image.png with your image extension
Center(
child: CircleAvatar(
radius: 100,
backgroundColor: Colors.blue,
child: Image.asset('assets/image01.png'),
),
),

You have to specify the extension of your image (i.e. image01.png)
child: Image.asset('assets/image01.png')

First create folder - best name (images). Put your picture, all of the pictures you want, calling. Then go to the pubspec.yml page edit portion of the picture.
flutter:
uses-material-design: true //two space
assets: //same up space
- images/ //two speace after asset
Then go to your code add all paths. Example:
backgroundImage: AssetImage("images/ss.jpeg"),

Related

image is not loading in flutter

this is my pubspec file this is happening every time I want to use the assets image. i use every combination for in asset
name: profile1
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.16.2 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0
flutter:
uses-material-design: true
assets:
- assets/*
[enter image description here][1]
[1]: https://i.stack.imgur.com/EJ75N.png
the error is in my pubspec file I cant use my asset images
Error on line 61, column 4: Expected a key while parsing a block mapping.
╷
61 │ assets:
│ ^
╵
Please correct the pubspec.yaml file at
C:\Users\Akshay\AndroidStudioProjects\profile1\pubspec.yaml
Process finished with exit code 1
dart code go to the image part
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: profile(),
));
class profile extends StatelessWidget {
const profile({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Text('profile'),
),
body: Padding(
padding: EdgeInsets.fromLTRB(19, 15, 0, 0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
using image in image asset Image but the error is in the pubspec file. i use various methods in dart that is not working
child: CircleAvatar(
backgroundImage: new AssetImage("assets/img1.jpeg"),
// backgroundColor: Colors.cyanAccent,
radius: 70,
),
),
),
),
backgroundColor: Color.fromARGB(255, 26, 26, 26),
);
}
}
The pubspec.yaml is indentation sensitive.
assets must be without any indentation at the start of the line. The individual asset entries are one tab level deeper in.
Pubspec.yaml is not set properly.
You should make the asset part as follows :
This is just an example. If you want, you can also export the whole folder as just lib/img/ . The important thing here is the alignment of the codes.

Not able to load image in flutter package

I've created a flutter package and images are inside images folder.
flutter:
uses-material-design: true
# To add assets to your package, add an assets section, like this:
# assets:
assets:
- images/
- images/location_pointer.png
I'm trying to load this image:
Widget build(BuildContext context) {
return Scaffold(
body: Image.asset(
'images/location_pointer.png',
width: 22.0,
height: 44.0,
fit: BoxFit.fill,
));
}
I've created a project and importing a package inside it:
dependencies:
flutter:
sdk: flutter
abc_pkg:
path: /Users/mosh/Documents/flutter proj/abcPackage/abc_pkg
I'm able to load the package when run this project but not able to load the images inside the package.
======== Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/location_pointer.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:675:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "images/location_pointer.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#092c6(), name: "images/location_pointer.png", scale: 1.0)
====================================================================================================
Make sure all your images are saved inside a directory called 'images', which is inside a directory called 'assets'.
Update your pubspec.yaml as follows:
flutter:
uses-material-design: true
# To add assets to your package, add an assets section, like this:
# assets:
assets:
- assets/images/location_pointer.png
Update your dart file as follows:
Widget build(BuildContext context) {
return Scaffold(
body: Image.asset(
'assets/images/location_pointer.png',
width: 22.0,
height: 44.0,
fit: BoxFit.fill,
));
}
please try this code. and make an image folder in the same
directory where you have the lib folder
in your pubspec.yaml file
flutter:
uses-material-design: true
assets:
- images/
and in your dart file
Widget build(BuildContext context) {
return Scaffold(
body: Image.asset(
'images/location_pointer.png',
width: 22.0,
height: 44.0,
fit: BoxFit.fill,
));
}

Unable to display images on Flutter app when using Scaffold Class

I am writing code for a simple flutter app.
The code is very simple,
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.teal,
shadowColor: Colors.black38,
title: Text(
'Some Ramdom Text',
style: TextStyle(color: Colors.black),
),
),
body: Center(
child: Image.asset('images/poor.png'),
),
),
),
);
}
However when I run it, I get the below error:
The following assertion was thrown resolving an image codec:
Unable to load asset: images/poor.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
<asynchronous suspension>
What could I be missing? I tried refactoring the project.
I checked the pubspec.yaml, it looks good.
assets:
- images/
The Error states itself It was not able to find the assets or load it do a pub get in pubsec.yaml after saving it.
It will fix your problem
I am using Android Studio, and when I open pubspec.yaml, I see the option Pub Get on the right top corner on the screen.
This enables the project to install the referenced packages.
After this I was able to view the image.

How to load an image using flutter

I'm new in flutter, just started since last week. I'm learning from the online lesson, then i want to load a image in my flutter app. But there was an error which is :
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/image2.jpg
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:225:7)
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:670:14)
Image provider: AssetImage(bundle: null, name: "assets/image2.jpg")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#9f84b(), name: "assets/image2.jpg", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
Below is the main.dart code :
import 'package:flutter/material.dart';
void main() =>
runApp(MaterialApp(
home: Home(),
));
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar : AppBar(
title : Text(
"Welcome to HLH",
style: TextStyle(
fontSize: 20.0,
color: Colors.black,
fontFamily: "Goldman",
),
),
centerTitle: false,
backgroundColor: Colors.amber,
),
body: Center(
child: Image(
image : AssetImage("assets/image2.jpg"),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Text("Login"),
),
);
}
}
Can anyone exists me how to solve it? I'm wondering is it the image problem? Thanks^^
This can be a error of not declaring the assets in your pubspec.yaml file.
flutter:
assets:
- images/
Add this to your pubspec.yaml file and then flutter pub get on the app root folder in terminal
I think this problem was caused due to the image path!
Make sure that the path of the image correctly in pubspec.yaml (must be the same with the image path directory).
like this:-
i saved my image in this path "assets/images/cat.png"
Pubspec.yaml file :
flutter:
assets:
- assets/images/

Image not being Shown for Flutter AppBar

The images for the code below are not showing and it's throwing an exception. My pubspec is aligned correctly so not sure what's wrong.
I tried updating flutter, restarting,re-aligning but still get the same exception below.
Syncing files to device iPhone X... flutter: ══╡ EXCEPTION CAUGHT BY
IMAGE RESOURCE SERVICE
╞════════════════════════════════════════════════════ flutter: The
following assertion was thrown resolving an image codec: flutter:
Unable to load asset: /images/barlogo.png flutter: flutter: When the
exception was thrown, this was the stack: flutter: #0
PlatformAssetBundle.load
(package:flutter/src/services/asset_bundle.dart:221:7) flutter:
import 'package:flutter/material.dart';
void main() {
return runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: new Color(0xffA62FF0),
centerTitle: true,
elevation: 1.0,
leading: new Icon(Icons.camera_alt),
title: SizedBox(
height: 35.0, child:new Image.asset('/images/barlogo.png')),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 12.0),
child: new Image.asset('/images/search.png'),
)
],
),
)
)
);
}
The exception gives it away - Flutter cannot find the image requested.
You should not have a leading / when specifying asset paths. Try:
Image.asset('images/barlogo.png')
There's more info in the documentation: https://api.flutter.dev/flutter/widgets/Image/Image.asset.html