Flutter Unable to load asset - flutter

I could find this issue as quite common, but any of the suggestions didn't help me.
I want to simply show an image:
body: new Image.asset('rh.png',width:300,height:100),
You can see it is in the correct folder
It has correct place in pubspec.yaml
But I still get an error:
Unable to load asset: rh.png
I tried a lot of suggestions from github or stackoverflow, but none of them helped me; played with spaces and tabs in .yaml file (doing flutter clean after each attempt), tried to rename .png file and put it in the subfolder - no success. Where is the catch?

You didn't add assets with the filename:
Correct way is: new Image.asset('assets/rh.png', width:300, height:100)

Complete image path should be passed for fetching any asset file
in your case using assets/rh.png would resolve the error

The problem is with your path to your .png file.
It should address properly to the file location.
Correct form would be something like below:
new Image(image: AssetImage('assets/rh.png')),

Related

How to add an image to github ReadMe.md file

I have tries all the below and the image does not seems to be appearing on my read me file. I've added the image under the issues as someone suggested but nothing. also tried numerous other methods but the image does not want to appears and on the video tutorials i'm doing line by line but nothing.
link to the isssues:
https://user-images.githubusercontent.com/89415628/185356092-4229772b-1852-4ee0-9daa-043ff87a2080.png
using the img tag with the image that are in the root folder
<img src="https://github.com/Opzalas/CMPG-323-Overview---33448809/blob/main/branching.png?raw=true">
The most used method to add image to read me file with image under the issues
![](https://github.com/Opzalas/CMPG-323-Overview---33448809/blob/main/assets/images/89415628/185356092-4229772b-1852-4ee0-9daa-043ff87a2080.png?raw=true)
image stored on the root repo with image link
![](https://github.com/Opzalas/CMPG-323-Overview---33448809/blob/main/branching.png)
** Image with the root directory**
![](branching.png)
What is funny is that it is working within stack overflow but not on github with the img tag with root directory.
Answer provided in #Benjamin W's comment; thank you, much appreciated. Please see answer below:
I took a look at your file, and the problem is that your text blocks are indented. In Markdown, that means "code block", where everything is interpreted literally. You have to un-indent them. I have created a PR to show you how.

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

Azure Devops image in readme from relative path

I'm trying to add an image to my readme but I can't get it to render.
As far as I can see online my current setup should work.
My folder structure is like so:
And then I tried to use the image located in the resources directory, but that did not work:
![Architecture view for AllProjects.png](./Resources/Architecture-view-for-AllProjects.png)
Any suggestions?
This seems to work well for me: Replace the space with or %20.
![Architecture view for AllProjects.png](./Resources/Architecture%20view%20for%20AllProjects.png)
![Architecture view for AllProjects.png](./Resources/Architecture view for AllProjects.png)
Result:
Your file is Architecture view for AllProjects.png so if you add - all should be fine.
Please check out here - Spaces in path to image file.
I tried encoding it with %20 but link was still broken. So your only/best way is to add - to the name.

how to display animated gif in flutter?

I am trying to display gif in flutter.
I am using the code
Image(image : NetworkImage(message.image_url))
But it shows error:
Another exception was thrown: Exception: HTTP request failed, statusCode: 403, https://media.giphy.com/media/13AXYJh2jDt2IE/giphy.gif%20
Place your gif in your images folder of your project and mention it in pubspec.yaml file,just like you do for images.
Image.asset(
"images/loading.gif",
height: 125.0,
width: 125.0,
),
This is what used in Flutter Gallery app to display .gif from web
Image.network('https://example.com/animated-image.gif')
I found a way with which you can download and add giphy.com's gif in your application using #Nudge answer.
Here is the trick which you can use-
Sample Giphy's .gif URL - https://giphy.com/gifs/congratulations-congrats-xT0xezQGU5xCDJuCPe
You have to split the last part of url after -.
Insert the last part in the following url: https://giphy.com/media/{URL_PART}/200.gif.
In this case, URL_PART will be xT0xezQGU5xCDJuCPe. Simply replace it in the above url.
You will get the following output url:
https://giphy.com/media/xT0xezQGU5xCDJuCPe/200.gif
Check it out. I hope it will help someone. ;)
EDIT:
Recently, Giphy changed their url format to https://i.giphy.com/media/${URL_PART}/200.gif (Special thanks to #Rivaan for mentioning it out).
Now, new url would become: https://i.giphy.com/media/xT0xezQGU5xCDJuCPe/200.gif
Giphy does not allow you to load the image. There's nothing that flutter can do about it: https://en.wikipedia.org/wiki/HTTP_403
When adding a new resource to assets, restart the ide
An update to #Chetan Goyal 's answer. It throws an exception of incorrect image data now.
To display URL from Giphy that looks something like https://giphy.com/gifs/congratulations-congrats-xT0xezQGU5xCDJuCPe, we need to extract the part after hyphen i.e. xT0xezQGU5xCDJuCPe and add it to this URL: https://i.giphy.com/media/${URL_PART}/200.gif
Hope it helps someone.

Swift : WKWebView not showing image on device

I am loading a html file to WKWebview from local everything is working fine but it is not showing image on iPhone.
This is the code i am using to load a html file
do{
let fileName = try String(contentsOf: destinationURLForFile)
print("fileName is equal to\(fileName)")
webView.loadHTMLString(fileName, baseURL: destinationURLForFile)
}catch{
}
Trying to fix since hours. Any suggestions would be helpful!
I had a very similar issue yesterday, you need to tell the build to copy this file to the device as part of the bundle.
Make sure sure you have the HTML file in the Copy Bundle Resources part of the build settings. Just go to the project settings > build phases > copy bundle resources and if your HTML file is not listed there, click the + button and add it.
This should solve your issue, if not could you please provide more information, are you getting any errors, does the file actually exist?