Not able to load image in flutter package - flutter

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,
));
}

Related

Unable to load asset: assets/image1 (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"),

Flutter Dart: can't load SVG assets: Unable to load asset iconPath

I am trying to use the flutter_svg package to load some .svg icon.
class _MyHomePageState extends State<MyHomePage> {
final String iconPath = "assets/icons/adept.svg";
...
}
I added the assets/ folder containing the icons/ folder to the pubspec.yaml file:
assets:
- assets/
And when I try to load the icon inside my body:
body: Center(
child: Container(
child: LimitedBox(
child: SvgPicture.asset('iconPath', color: Colors.black, width: 100, height: 100,),
maxHeight: 100,
maxWidth: 100,
)
)
),
I get this StackTrace, Unable to load asset: assetName,
I/flutter (10154): ══╡ EXCEPTION CAUGHT BY SVG ╞═══════════════════════════════════════════════════════════════════════
I/flutter (10154): The following assertion was thrown resolving a single-frame picture stream:
I/flutter (10154): Unable to load asset: iconPath
I/flutter (10154):
I/flutter (10154): When the exception was thrown, this was the stack:
I/flutter (10154): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:223:7)
...
I/flutter (10154): Picture provider: ExactAssetPicture(name: "iconPath", bundle: null, colorFilter: null)
I/flutter (10154): Picture key: AssetBundlePictureKey(bundle: PlatformAssetBundle#153a9(), name: "iconPath",
I/flutter (10154): colorFilter: null)
I/flutter (10154): ════════════════════════════════════════════════════════════════════════════════════════════════════
Kindly explain to me what am I missing?
Did you change like below code?
assets:
- assets/
- assets/icons/
body: Center(
child: Container(
child: LimitedBox(
child: SvgPicture.asset(iconPath, color: Colors.black, width: 100, height: 100,),
maxHeight: 100,
maxWidth: 100,
)
)
),
just do 3 work:
Run this command in terminal : flutter clean.
and in android studio go to file and select:
Invalidated caches and restarted.
then get dependencies again by this command: flutter pub get

Unable to load asset in Alert Dialog content

I know that there are a lot of thread related to this question but they didn't help.
I have an asset folder containing images. Images are properly shown in other parts of the code but not in the Alert Dialog. In the example I use a .gif file, but I had the same problem also with .png.
Error from Debug Console
════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: check.gif
When the exception was thrown, this was the stack
#0 PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:225
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:668
#2 AssetBundleImageProvider.load
package:flutter/…/painting/image_provider.dart:651
#3 ImageProvider.resolveStreamForKey.<anonymous closure>
package:flutter/…/painting/image_provider.dart:504
...
Image provider: AssetImage(bundle: null, name: "check.gif")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#234cb(), name: "check.gif", scale: 1.0)
pubspec.yaml
flutter:
uses-material-design: true
assets:
- assets/
I already tried using /assets/check.gif but I had the same problem.
Code
showDialog(
context: context,
builder:
(BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius
.all(Radius
.circular(
13)),
side: BorderSide(
color: const Color(
0xff5387ff),
width: 3)),
content: Container(
height: MediaQuery.of(
context)
.size
.height /
9,
child: Column(
children: [
Image.asset(
'check.gif',
width: 20,
height: 20,
fit: BoxFit
.contain,
),
],
),
));
});
Output:
Hope you'll be albe to help me
You need to add your folder path. Like where you store your assets that folder path and your asset name. Like the below
Image.asset(
'assets/check.gif',
width: 20,
height: 20,
fit: BoxFit
.contain,
),
Try out this, I think you forgot to add "assets" in Widget
Image.asset('assets/check.gif', width: 20,height: 20,fit:BoxFit.contain,),

Image added to Appbar not showing - flutter

I have added an image to my appbar but it does not show when I run the app. Here is my code:
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('assets/images/Appbar_logo.png',
fit: BoxFit.cover, height: 56),
],
),
),
Here is the error:
Unable to load asset: assets/Appbar_logo.png
Here is the package structure in the project:
Project
lib
assets.images
Appbar_logo.png
Here is my pubspec.yaml file:
assets:
- images/
I think there is an issue with my package layout.
try including assets folder in the path in your pubspec.yaml file
assets:
- assets/images/
Your assets folder is inside the lib ? It should be on roots
Project
android
assets
ios
lib
test
...
And in your pubspec.yaml file:
flutter:
assets:
- assets/images/Appbar_logo.png

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/