Flutter image is not showing - flutter

I want to show some local image and display it on the screen like this:
Image(image: AssetImage('assets/images/addFood.png'))
But I cannot see the image on the screen and I am getting no error at all.
Here is what I have done:
In my pubspec.yaml I have added my image folder like this:
assets:
- lib/assets/images/
The image named "addFood" is inside images folder as you can see here:
I want to display a simple text with image under it, I can see the text on the screen and I am doing it like this:
Widget build(BuildContext context) {
if (favoriteMeals.isEmpty)
return Column(children: <Widget>[
Text('no favorites',style: Theme.of(context).textTheme.title,),
Image(image: AssetImage('assets/images/addFood.png'))
]);
else{
......
......
......
}
All I can see is the text without the image:
solutions that did not help in my case:
AssetImage is not not displaying image in flutter app
Adding assets and images
mage Not Appearing when Using "new Image.asset" inside a Column
How can I fix it and make sure that the image will be displayed on the screen?

You need to provide the full path for an asset when accessing it within the code. Nothing is implied when accessing the assets. Your actual path is lib/assets/images/, but you only do assets/images/. Change your code to include this:
Image(image: AssetImage('lib/assets/images/addFood.png'))

It should be
flutter:
assets:
- assets/images/
Remove lib from pubspec.yaml path. And to get image in code use,
Image.asset('assets/images/lake.jpg')

Update pubspec.yaml file like this
assets:
- directory/subdirectory/

Related

Display an image from the asset in the documentation

I'm writing documentation about a variable and I would like to include an image that is inside the project. For example:
assets/
|- icons/
| |- my-image.svg
lib/
|- my_file.dart
I know it is possible to display an image from an URL, but what about one from a file?
Those were my unsuccessful tries:
// lib/my_file.dart
/// The image attempt:
/// ![](assets/icons/my-image.svg)
/// ![](../assets/icons/my-image.svg)
const myVariable = 0;
But it doesn't work:
Is there a way to do it?
There is already an issue on GitHub: #2390
and also a related thread: Using local image assets in dart documentation comments
Effectively, web urls are working, but relative paths aren't, because VSCode doesn't support that. It tries to open the files relative to its application directory. But I can't confirm that for Windows either, because for me, not even absolute paths are working...
So if your project lives in a public repository anyways, you could just use a web url.
1:open pubspec.yaml and click "Pub get"
2:
Image.asset("assets/icons/my-image.svg",width: 100,height: 100,)
So i think you want to let the variable hold the asset image path so do this
Go to pubspecs.yaml file and add this
flutter:
assets:
- assets/icons/my-image.svg
Then run flutter pub get
then you can use the image this way
//let the variable hold the image path
const myVariable = "assets/icons/my-image.svg";
//to display on widget
Image.asset(myVariable);

How can add a specified image asset to pubspec and then load the image to AssetImage for the app to present?

I can't find anywhere teaches how to add a specified image within asset. Actually I don't know which pictures are included in the asset, or if I want to use an image from the Internet or my own photo gallery, I don't know how I can link the picture with the asset and apply it in my code.
Similarly, I also don't know what are included in packages, like font_awesome flutter. How can I check what are included? Thank you!
I think I have solved the above questions with the help of commenters, but I have more questions coming up: Why I was unable to load the asset?
Please see the pictures below.
enter image description here
enter image description here
you can place your assets in a directory in your project folder.
For example assets\drawables\ and assets\icons\
Then in your pubspec.yaml you should declare that folder. In this example:
# To add assets to your application, add an assets section, like this:
assets:
- assets/drawables/
- assets/icons/
Than in your code you can use them as follow:
Image.asset(
'assets/drawables/image.png',
width: 200,
),
For your second question, I think you only have two options:
either look for the library documentation / website, such as www.fontawesome.com (the free icons are the one in the library). Or look in the code/package yourself
Place your picture in a folder named assets like this:
Add this line to your pubspec.yaml:
Then put this somewhere in your code:
Image.asset(
'assets/my_image.png',
width: 200,
height: 200,
fit: BoxFit.contain,
);

Flutter web doesn't recognise pdf file format

I'm trying to show the pdf file when pushing the button but no success so far. Can there be the reason that the format of the pdf file has that question mark on the icon meaning the framework doesn't recognise the format?
No, the Assets folder can contain any file regardless of the file extension. Have you define your assets folder in pubspec.yaml like this?
assets:
- assets/pdf/
If yes you can use native_pdf_view library as it supports asset loading as well as full screen. You should be able to implement it as follows:
final pdfController = PdfController(
document: PdfDocument.openAsset('assets/copy.pdf'),
);
return Scaffold(
body: Center(
child: PdfView(
controller: pdfController,
)
),
);
Original link to the post is here.

Flutter crashing on failed PlatformAssetBundle

Running my app I get a crash, but stack trace doesn't go deep enough because of an Async call? I cannot figure out what is trying to grab an image?
Below is the assets part of my pubspec.yaml file
assets:
- assets/
- assets/images/
Using VSCode and it shows any images I reference or if I mouse over the reference it shows the images.
#protected
Future<ui.Codec> _loadAsync(AssetBundleImageKey key, DecoderCallback decode) async {
ByteData data;
// Hot reload/restart could change whether an asset bundle or key in a
// bundle are available, or if it is a network backed bundle.
try {
data = await key.bundle.load(key.name);
} on FlutterError {
PaintingBinding.instance.imageCache.evict(key);
rethrow; <---- crashes here
}
I am not grabbing any external images (URL) in my code. I added Google Maps SDK so maybe its something it is trying to grab? But not sure why no stack trace?
VSCode see the images before I send it to the iOS simulator. These are the images that it cannot find. It finds all other images referenced in the file.
Below is the assets part of my pubspec.yaml file
assets:
- assets/
- assets/images/
Try to follow the indentation of the documentation.
Here is an example:
flutter:
assets:
- assets/my_icon.png
- assets/background.png
If the error still exist, try to follow the workaround here:
It worked for me:
Set only full path to asset in pubspec.yaml
e.g.
assets:
- assets/files/asset.txt
- assets/images/icons/myicon.png
Whole folder not worked for me - assets/
Add ./ to the beginning of your asset path when you loading it
e.g. rootBundle.loadString('./assets/path/to your/asset.txt
flutter clean
Also, as mentioned in this comment, using flutter clean is a good idea if there are some weird behavior from your app.

How to add images in vscode - Flutter

I am trying to add images to an asset folder in VSCode, but I only get the option to type the image name. When I do and click on the newly created image, it says "an error has occurred." I presume this is because I am only typing the file name, not adding the actual image.
I added the full path to the yml file, but nothing loads in the app.
assets:
- C:/Users/tt/Desktop/Flutter_projects/Practise/imaages/images/assets/android.png
Flutter code:
child: Image(
image: AssetImage('assets/android.png')
),
),
use relative path to indicate the folder(e.g.myImages) that hosts your image assets.
then
flutter:
assets:
- myImages/android.png
see flutter doc for more info:
Adding assets and images