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
Related
I am working on a flutter web Project, I need to display some countries as tiles like below. The Flag Image is is displayed when I run them in release or debug mode, But after I deployed them to firebase, The image is not displayed, and not any errors are displaying while I was checking in debug mode.
I have tried building in html renderer also, it didn't help.
The image I'm trying to display is from this package,
https://pub.dev/packages/country_icons
Weirdly, The same widget is working good on another screen within the same application.
I have tried importing the flag pngs to local and made them as assets, still I got the same output, Instead of the below widget I used ListTile with the leading flag, same behaviour.
This is when I run in debug/release mode
This is after I deployed the code into firebase.
pubspec.yaml below,
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
country_icons: ^2.0.2
Below is my widget.
Widget build(BuildContext context) {
return AspectRatio(
aspectRatio: 3,
child: Card(
shadowColor: Colors.grey[600],
elevation: 5,
color: Colors.white,
child: Row(
children: [
Expanded(
flex: 1,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Image(image: AssetImage('icons/flags/png/$code.png', package: 'country_icons')),
),
),
Expanded(flex: 3, child: Text(text)),
],
),
),
);
}
Any help is appreciated, Thanks
Adding a toLowerCase() method solved the problem, Turns out, fetching being case insensitive in local, but after deployed, the file names are behaving as case-sensitive.
CountryCard(
text: e.name,
code: e.code.toLowerCase(),
),
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.
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,
));
}
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"),
I have a folder of assets called stickers. The folder contains images (.png, .jpeg) and gifs (.gif). I want to show the user all his stickers.
My stickers folder:
My current code supports png files only:
children: List.generate(30, (index) {
return Padding(
padding: const EdgeInsets.all(15.0),
child: Image(
image: AssetImage(
'assets/stickers/sticker_${index + 1}.png',
),
),
);
}),
How do I list assets, based on names, ignoring the file suffix?
Or how to support wildcard assets in flutter?
you can copy the assets names with a mixed sort in a list like this :
const List assetslist = const [
'sticker_1.png',
'sticker_2.png'
'sticker_3.jpeg'
'sticker_4.png'
'sticker_5.gif'
'sticker_6.png'
];
then use it to generate an image for each of text:
children: assetslist.map((e) {
return Padding(
padding: const EdgeInsets.all(15.0),
child: Image(
image: AssetImage(
e,
),
),
);
}).toList(),
You can add assets by wildcard by adding this at the pubspec.yaml.
assets:
- assets/stickers/
But, for wildcard assets, you can't use hot reload if you update those assets inside that folder, you need completely stop the app and start again via android studio or vs code.