Instead of seeing my image in my app I'm seeing a red box with an X that says Unable to load asset: images/aboutmaggie.png.
I made a directory assets with a subdirectory images. The directory assets is at the same level as pubspec.yaml.
I put the image into the images directory. When I click on the image in Android Studio the image displays.
In pubspec.yaml I have these lines:
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/
I added
class AboutRoute extends StatelessWidget {
const AboutRoute({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Kabbalistic Numerology'),
),
body: ListView(
shrinkWrap: true,
padding: const EdgeInsets.all(8),
children: <Widget>[
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: 'About Maggie McPherson',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
)),
],
),
textAlign: TextAlign.center,
),
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text:
"Dr. Leslie Margaret Perrin McPherson...",
style: TextStyle(
color: Colors.black,
)),
],
),
),
Image.asset('images/aboutmaggie.png'), // <--this image doesn't load
]));
}
}
I ran flutter pub get. I ran Tools > Flutter > Flutter Clean. I shut down and restarted Android Studio.
The error message is:
======== Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/aboutmaggie.png
I tried putting another image into assets/images and calling that but the second image wouldn't load either.
What's wrong with this picture?
there is an error in calling the image,
change this line in your code
Image.asset('images/aboutmaggie.png')
to this line
Image.asset('assets/images/aboutmaggie.png')
I hope this helps, thankyou
Try like this:
Image.asset('assets/images/aboutmaggie.png'),
Try below code hope its helpful to you.
Refer documation here for Adding assets and images
Refer Image Class here
Your folder structure
project directory
- assets
- images
- your_image.png
Your pubspec.yaml file
flutter:
assets:
- assets/images/
Your Widget:
Image.asset(
'assets/images/ln.png',//just change your image to my image
height: 150,
//width: 100,
),
Note - If you adding correct code and details then your problem not resolved then stop(exit) the project/app and relaunch again
Result Screen->
You need to provide complete path of the image.
Replace
Image.asset('images/aboutmaggie.png')
with
Image.asset('assets/images/aboutmaggie.png').
It was cacheing issue. Last night I tried
Image.asset('assets/images/aboutmaggie.png'),
but that didn't work. This morning I started up Android Studio and it asked,
pubspec.yaml has changed. Do you want to run flutter pub get?
Well, that's odd. I'd just started Android Studio. I hadn't changed anything. I changed the path back to 'assets/images/aboutmaggie.png', ran flutter pub get, did a hot restart and the image appeared.
Then I opened pubspec.yaml and changed it to
assets:
- assets/bowlingballs/
I ran flutter clean, flutter pub get, did another hot restart, and the image continued to appear.
I changed the path back to 'images/aboutmaggie.png', ran flutter pub get, did a hot restart and the image was gone.
My conclusion is that if you change this path
Image.asset('assets/images/aboutmaggie.png'),
then run flutter pub get and a hot restart then the change will appear. But if you make changes to pubspec.yaml you won't see changes until some cache is cleared. The problem I had last night was that when I set up pubspec.yaml I had an extra space before assets. That version of pubspec.yaml got cached and subsequent versions of pubspec.yaml were ignored. flutter clean clears the Build cache but pubspec.yaml apparently isn't in the Build cache. pubspec.yaml must be cached somewhere else.
In your terminal, run flutter clean to clear up the cache, then run flutter pub get for flutter to fetch your assets folder afresh
This is what worked for me
Related
In the Container widget, I have tried implementing many flutter icons and all are working well except for the cell_tower_rounded. This is an icon that is as well supported by icons Null safety.Kindly help why is this cell_tower icon not working while the rest are just ok.
My editor underlines it in red and the error is undefined_getter
Container(padding,child,childred[
buildMenuItem(
text: 'Connection Status',
icon: Icons.cell_tower_rounded,
onClicked: () => selectedItem(context, 0),
]),
I think you have some typo on your code. The correct should be
Container(padding,child:Row(children[
buildMenuItem(
text: 'Connection Status',
icon: Icon(Icons.cell_tower_rounded),
onClicked: () => selectedItem(context, 0),
])),
You can use either Row or Column. It depends on what you want.
Thank you for your contributions, But well, I figured it out and found a fix. The problem was that my Flutter Version was out of date. Most of the latest icons are not easily supported in previous flutter versions.
I was using Flutter: 2.8.2 while at the time of release of cell_tower_rounded icon, flutter version was at Flutter: 2.10.2.
Incase you are experiencing a unique problem with some widgets, packages or icons, simple run: $ flutter upgrade in your project directory.
You'll probably also need to update your Kotlin Version in the android/build.gradle to the latest one.Find the latest Kotlin version here:
Today(as at the time of this post), the latest one is version 1.6.0, so kindly update like this:
buildscript {
ext.kotlin_version = '1.6.0'
I have added my asset files in my projects root directory, which contains images, fonts and sounds.
added the assets in pubspec.yml file like this:
flutter:
uses-material-design: true
assets:
- assets/images/
Then I try to add an image through Image.asset like code below:
Scaffold(
appBar: AppBar(
leading: Align(
alignment: Alignment.centerRight,
child: Text(
"Uber",
style: TextStyle(
fontSize: 22.0,
fontWeight: FontWeight.w400
),
)
),
),
body: Column(
children: [
Text(
"riyad",
style: TextStyle(color: Colors.black),
),
Image.asset("car_android.png") <-- this like
],
),
);
but it shows me exception that failed to load asset:
The following assertion was thrown resolving an image codec:
Unable to load asset: car_android.png"
My traceback exception:
════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: car_android.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: "car_android.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#243b1(), name: "car_android.png", scale: 1.0)
I tried to run command flutter clean command to clean the cache, then run the app again, but the same problems occurs again, that it fails to load asset.
I am new to flutter please provide descriptory answers and also suggest me some blogs to read
Specify Image path as follows
Image.asset("assets/images/car_android.png")
OR
Image(image: AssetImage("assets/images/car_android.png"))
The problem is flutter can't recognize where your image is exactly stored.
If the problem continues then try to do HOT RESTART this will solve your problem.
If you update pubspec.yaml file then run flutter pub get to update the libraries if your IDE doesn't update it automatically.
Just to push a little further on Harshil Khamar answer which is correct and valid another consideration.
The assets in pubspec.yaml has to be under flutter if you place another entry in between these then it does not know where the assets folder is. It may sound stupid, but I am sure I am not the only one to have done this.
flutter:
uses-material-design: true
generate: true
flutter_intl:
enabled: true
assets:
- assets/images/<<imagename>>
The flutter_intl gets in the way as assets now thinks they is under flutter_intl and not flutter. If I am the only idiot then okay so be it.
flutter_intl:
enabled: true
flutter:
uses-material-design: true
generate: true
assets:
- assets/images/<<imagename>>
This solves the problem
Two problems can cause this problem:
you should stop your project and run it again without any HOT RELOAD
you should add a full directory of the image -> assets/images/YOUR_IMAGE.png
The problem you are having is because you did not give the full path to your asset in the creation of an Image object.
body: Column(
children: [
Text(
"riyad",
style: TextStyle(color: Colors.black),
),
Image.asset("assets/images/car_android.png") <-- this like
],
),
1.Check indentation. See image attached:
Image url path should be defined like this:
Kill and reload app. This seems essential after multiple checks of 1 and 2.
Image.asset("assets/images/car_android.png")
flutter pub clean
flutter pub get
flutter run
I am using the rFlutter_alert package, but when I try to add an image, the image does not appear. This is the function I am using:
_onAlertWithCustomImagePressed(context) {
Alert(
context: context,
title: "RFLUTTER ALERT",
desc: "Flutter is more awesome with RFlutter Alert.",
image: Image.asset("assets/success.png"),
).show();
}
The image (success.png) is in the assets folder of the project and I added the image to pubspec.yaml:
flutter:
uses-material-design: true
assets:
- assets/success.png
This is the result I am getting:
Edit:
The issue has been solved since V2.0.3 according to the author comment in the issue.
There is an open issue for this problem which is opened in the GitHub repo, you can check it out via this link Custom Image doesn't show up #108
First, I added the google_fonts package to your pubspec dependencies.
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
google_fonts: ^0.2.0
Then
import 'package:google_fonts/google_fonts.dart';
and apply to a Text widget
Text(
'This is Google Fonts',
style: GoogleFonts.lato(fontSize: 40),
),
Text(
'This is Google Fonts',
style: GoogleFonts.adventPro(fontSize: 40),
),
Please check internet connection- your emulator does not have internet connectivity. google fonts need internet connection on device/emulator.
After adding the dependency pubspec.yaml file
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
google_fonts: ^0.2.0
run the command in terminal\cmd console as :
> flutter packages get
this will fetch the dependency into your workspace.
Google Fonts are fetched from the internet in the runtime. So you observe this behaviour
To overcome this, download the font from google font and make it available in the asset folder by following the steps below.
Visit the https://fonts.google.com/ and download the lato font.
At the root directory, create a directory called google_fonts.
Copy-Paste lato.ttf file into the google_fonts folder.
Open the pubspec.yaml file, Under the assets: section add the -google_fonts/
And change the code from
// Online Mode
Text(
'This is hammersmithOne from Google Font'
style: GoogleFonts.lato(),
),
to
// Offline Mode
Text(
'This is hammersmithOne from Google Font',
style: TextStyle(fontFamily: 'lato') // This is loaded from assets
),
For futher reference refer this article.
Refer these stackoverflow answers aswell:
https://stackoverflow.com/a/74949875/13431819
https://stackoverflow.com/a/74950261/13431819
I fixed mine by deleting the emulator and installing a new one. My older emulator was not connecting to the internet for some reason and reinstalling it fixed it for me.
Unless you store the fonts in your assets, you need to define explicitly that runtime fetching is enabled for Google Fonts:
GoogleFonts.config.allowRuntimeFetching = true;
I'm currently working on a side project and when I have included a font-awesome icon inside my app, it does not show up and instead, a blank box with two lines intersecting with each other like the letter x, how to fix this?
class ImageHoldingComponent extends StatefulWidget {
#override
_ImageHoldingComponentState createState() =>
_ImageHoldingComponentState();
}
class _ImageHoldingComponentState extends State<ImageHoldingComponent> {
#override
Widget build(BuildContext context) {
return Container(
child: Icon(FontAwesomeIcons.python)
);
}
}
The author of font_awesome_flutter package suggests the following steps for anyone who has icons not showing problem:
Stopping the app
Running flutter clean in your app directory
Deleting the app from your simulator / emulator / device
Rebuild & Deploy the app.
Also make sure to set uses-material-design to true in pubspec.yaml file.
This happens when you start using the older version of font_awesome_flutter and because the virtual device cache is stored when you build the Flutter app, so every time you change the icon you have to stop the emulator and rebuild it.
My experience is:
Stop main.dart (Android Emulator)
In pubspec.yaml add (font_awesome_flutter) new version
font_awesome_flutter: ^8.8.1
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: ^0.1.2
font_awesome_flutter: ^8.8.1 //new verison
And Then click
"Pub get"
at the top of Flutter commands
Run the emulator again (start main.dart)
If your app is running on the emulator or device just
stop it and relaunch it again
it solves your problem
Just save the from pupspec.yaml file under dependices and say pub get, don't forget to import it
Add font_awesome_flutter: ^10.3.0 package in pubspec.yaml file.
Then use like this
FaIcon(
Icons.access_alarm,
color: whatsappColor,
size: 30,
),