From an AssetImage object i'm trying to get the file name in Flutter.
Image photo = Image.asset('assets/images/image-not-found.jpg');
print(photo.image); // AssetImage(bundle: null, name: "assets/images/image-not-found.jpg")
print(photo.image.assetName); // assetName not defined
print(photo.image.name); // name not defined
How to get this assetName property?
Something nicer without doing some regex on photo.image.toString();
EDIT:
the purpose here was to retrieve the image name in order to compare it.
So i just go for direct comparaison.
if(photo.image == AssetImage('assets/images/image-not-found.jpg')){
print("image was not found");
}
Try AssetImage for this.
var photo = AssetImage('assets/images/image-not-found.jpg');
print(photo.assetName);
//You can use split method like
`String toSplit = 'assets/images/image-not-found.jpg';
var imageName = toSplit.split("/");
print(var[2]);`
//You can get your image name stored in var[2].
First, add an asset folder in pubspec file like:
assets:
- specify a folder where is your assets are stored.
and use like this
Image.asset(
'assets/images/profile.png',
fit: BoxFit.fill,
),
Related
i was wondering how to retrieve an image from Firestore, i was trying and reading and watching tutorials so i have come with this solution:
1- Upload images to Firebase storage.
2- Go to Firestore create a collection and a document with field name and value of image path. ( collection "majorChallenges", doc "challenge-1", field "source", value "image path url")
3- Initiate a Firestore Instance: var _fireStoreInstance = FirebaseFirestore.instance;
4- Access the Database collection using QuerySnapshot: QuerySnapshot qn = await _fireStoreInstance.collection("majorChallenges").get();
5- now after that am not sure how to setState, initState, return and display the image inside Scaffold or Container.
some tutorials are using the image link or name which is not efficient for me what if i have to change the image in the future so the best solution i believe must not use the image name it should access the collection and the doc and retrieve the image path.
i already set up the read and write permissions and i have accessed the database before to retrieve text so no authorisation problems, I would be very thankful if someone could complete the steps for us
I can now answer my own question, first of all it is wrong to specify this operation in steps because there are a lot of approaches to achieve this goal, and the approach i am using is pretty nice and simple using QuerySnapshot. so to retrieve picture we need simply do these steps (for my approach):
1- we need to declare a string variable then create asynchronous method that will fetch the image URL from Firestore and update the variable state using set state method, inside this method we can initiate a Firestore instance and a QuerySnapshot:
String img = "";
fetchMajorChallengeImage() async {
var _fireStoreInstance = FirebaseFirestore.instance;
QuerySnapshot qn =
await _fireStoreInstance.collection("majorChallenges").get();
setState(() {
img = qn.docs[0]["source"];
});
return qn.docs;
}
2- we will initiate the state using initState method and call our function inside:
void initState() {
fetchMajorChallengeImage();
super.initState();
}
3- now the image is retrieved and the path stored to "img" variable we declared, so we need to display the image, here its your choice where and how to display the image but for me this is how i displayed it:
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Image.network(img, height: 150, fit: BoxFit.fill),
],
),
);
}
note that it must be displayed inside Image.network() widget because the variable holds a URL value fetched from the firestore collection > doc > value and now the image is retrieved and displayed successfully.
I pick a picture using this library image_picker: 0.8.4+4. Then the image saved into List..
After that i want to show the image in next page and make it ready to edit using this library image_painter: 0.4.4+1.
ImagePainter.file(
// Need file,
key: _imageKey,
scalable: true,
initialStrokeWidth: 2,
initialColor: Colors.green,
initialPaintMode: PaintMode.line,
),
Actually i just need how to convert List<XFile> to File
One way to do it:
XFile has a property called path so the you can:
XFile xfile = ...;
File file = File(xfile.path);
If you have a list, you can use map or other way you prefer to create File using the path of a XFile.
I was trying to create an app using the flutter desktop implementation that would read the path to an image from a JSON file and display the image, and then when an Inkwell is hovered, it would change the path to another image. The following was the declaration of variables:
characterName = 'Elise';
var skill_1_icon_path =
characterData[characterName]['ability 1']['icon path'];
I was changing the characterName variable to switch to other image paths.
The following widget was being used:
Inkwell(
child: Image(
image : AssetImage(
skill_1_icon_path),
),
onHover: (value){setState(){characterName = "Jhon";}}
),
)
However, the image does not change despite changing the path. I have tried printing skill_1_icon_path in both before hovering and after, the path changes to it's correct value, but the image itself doesn't.
In your onHover, use setState to also change skill_1_icon_path to the desired new image path.
The original question comes from flutter issue 32799
I develop a dart package, i need load some json file in runtime, but when I do this occur an error. load image is no problem ,the code:
void main() {
Future<void> loadAsset() async {
String value = await rootBundle
.loadString('lib/src/assets/JsonConfig/test.json');
//Image img = Image.asset('lib/src/assets/default-logo.png');
}
test('adds one to input values', () async {
await loadAsset();
});
}
my pubspec.yaml file like this:
flutter:
# To add assets to your package, add an assets section, like this:
assets:
- lib/src/assets/default-logo.png
- lib/src/assets/JsonConfig/test.json
- lib/src/assets/
To load assets from packages, you should add the prefix 'packages/<package_name>/' for the key to making it works.
Such as how AssetImage do
/// The name used to generate the key to obtain the asset. For local assets
/// this is [assetName], and for assets from packages the [assetName] is
/// prefixed 'packages/<package_name>/'.
String get keyName => package == null ? assetName : 'packages/$package/$assetName';
https://github.com/flutter/flutter/blob/fba99f6cf9a14512e461e3122c8ddfaa25394e89/packages/flutter/lib/src/painting/image_resolution.dart#L146
So add the prefix 'packages/<package_name>/' for the key will work on the demo above:
String value = await rootBundle
.loadString('packages/<package_name>/lib/src/assets/JsonConfig/test.json');
I'm working with saved image from Url with Gallery Saver. Everything it's oke , i can insert image from URL, but i don't know how to fetch image from picture Directory .
It's different about getApplicationDocumentsDirectory() or getExternalStorageDirectory() ?
I want to display the image that was just saved.
Any solution ?
Save image Code :
void _testSaveImage() async {
String path = "${Urls.BASE_API_IMAGE}/berita/${widget.gambarBerita}";
GallerySaver.saveImage(path).then((bool success) {
print('Success add image $path');
}).catchError((onError) {
print('Error add image $path');
});
}
Location Image saved
Then just return the path you have and use a Image.file() widget to display it, lets say you have a Column():
Column(children: <Widget>[
[...]
Text('Here is my image:'),
if (path.isNotEmpty && path != null)
SizedBox(
height: 200,
width: 200,
child: Image.file(File(path),
),
Text('Caption of the image'),
[...]
),
Docs for Image class here
Load local saved images using path and use it
var file = new File('path');
Image.file(file )