How to fix "Exception caught by image resource service" - flutter

I have created a resources folder to store my images.
part of 'resources.dart';
class IconImages {
IconImages._();
static const String test1 = 'assets/images/test1.jpg';
static const String test2 = 'assets/images/test2.jpg';
static const String test3 = 'assets/images/test3.jpg';
}
I write them to the Map collection
final imagesIcon = <int, String>{
1: IconImages.test1,
2: IconImages.test2,
3: IconImages.test3,
};
and then pass the recorded images to the button.
icon: Image(image: AssetImage(imagesIcon.values.toString())),
// full button
Column(
children: [
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: Image(image: AssetImage(imagesIcon.values.toString())),
onPressed: () {},),
),
SizedBox(height: 40,),
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: Image(image: AssetImage(imagesIcon.values.toString())),
onPressed: () {},),
),
SizedBox(height: 40,),
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: Image(image: AssetImage(imagesIcon.values.toString())),
onPressed: () {},),
),
],
),
When I execute my code, I get the following exception
Everything works when I transmit the image in this form
icon: Image(image: AssetImage(IconImages.test2)),
But when I try to get a path from Map in this way, the above error is obtained
icon: Image(image: AssetImage(imagesIcon.values.toString())),

You are using your map wrong:
IconButton(
icon: Image(image: AssetImage(imagesIcon[1].toString())),
onPressed: () {},
),

I think it may be because you're passing icons as strings. Instead of using images with a class like:
ImageIcon(
AssetImage("images/icon.png"),
color: Colors.red,
size: 24,
),
Edit: after recreating the situation in my project I've found the reason behind that. You use imagesIcon.values.toString() to pass the value of an Icon. Did you check what this function returns? In my case it returned:
(assets/images/test1.jpg, assets/images/test2.jpg,
assets/images/test3.jpg)
No wonder the image is unreadable according to flutter because that is not a proper path or file.
Instead you can use your solution like this:
imagesIcon.values.elementAt(1) //0,1,2 whatever index you need from map

Related

How can I make image to active like button in flutter?

appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(Icons.menu, color: Colors.pink,),
onPressed: () {
print("menu button is clicked");
}
),
title: IconButton(
onPressed: () { print("run button"); },
icon: Image.asset('assets/images/Slimer_by_shinrider-dcqaiza.webp')),
),
In codes, there's a IconButton that I made for making image to button. but it doesn't appear image. Is there any incorrect thing in code? or Do I have to another method?
Check the assets path setting in pubspec.yaml. Also, check if the image is in the assets path that you set normally.
pubspec.yaml
assets:
- assets/images/
show documents
https://docs.flutter.dev/development/ui/assets-and-images
GestureDetector(
onTap: () {print('click on edit');},
child: Image(
image: AssetImage('assets/images/icons/edit-button.png'),
fit: BoxFit.cover,
height: 20,
)),
try to use with .png

How to add search filter option like this image in flutter

I want to add a filter option button without adding a search bar.
ScreenShot
You can do like this
InkWell(
onTap: () =>
child: const Icon(
Icons.tune,
color: Colors.grey,
),
),
Make use of IconButton like below
IconButton(
icon: Icon(Icons.tune),
onPressed: () {},
)

not able to define web links that will open when icon button is pressed in listview builder which is already under expantionbar

(question1) i am not able to think how to pass 3 weblink link ( google.com, yahoo.com, youtube.com) that will open when 3 corresponding icons in topic 1 are pressed in code on :
https://github.com/sandeepnarula999/FlutterProjects/blob/main/ListViewUnderExpansionTile
pls try to share the complete code if u are able to resolve my issue ..as i shared full code above...pls do
not share just steps of how to do coz that may add up to errrors when i try
(question2)
As per below image suggestion when i try to change code from line 7 to 10 in abve code link i get errors.. are u able to get zero error with this below approach in image
Solution : add three Flexible widgets in the Row widget ;)
if (root.children.isEmpty) {
return ListTile(
title: Text(root.title),
subtitle: root.subtitle == null ? null : Text(root.subtitle!),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: IconButton(
onPressed: () {},
icon: const Icon(Icons.auto_stories),
iconSize: 30,
tooltip: 'Documents',
),flex: 1,
),
Flexible(
child: IconButton(
onPressed: () {},
icon: const Icon(Icons.video_collection_rounded),
iconSize: 30,
tooltip: 'Videos',
),
flex: 1,
),
Flexible(
child: IconButton(
onPressed: () {},
icon: const Icon(Icons.favorite),
iconSize: 20,
tooltip: 'Shortlist',
),flex: 1,
),
],
),
);
}
enter image description here

How do I change a flatbutton.icon to a textbutton in Flutter?

I have made a flatbutton icon, but it seems this is depreciated in the current flutter version. I have tried to upgrade it to a text button, but it does not show the text next to the icon.
This is the original code that has to be adapted to become a text button. Should I have to define a class or function to make it so?
Row(
children: [
FlatButton.icon(
onPressed: () => print('Live'),
icon: const Icon(
icons.videocam,
color: Colors.red,
),
label: Text('Live'),
),
],
)
Thx! :)
you can use TextButton.icon() widget. here an example:
TextButton.icon(
onPressed: () => print('Live'),
icon: Icon(Icons.videocam_rounded),
label: Text('Live'),
),
and here the result:
You can use an IconButton widget:
Row(
children: [
IconButton(
onPressed: () => print('Live'),
icon: const Icon(icons.videocam, color: Colors.red),
),
Text('Live'),
]
)

how to do upload image within steps method in flutter

List<Step> steps = [
Step(
isActive: false,
state: StepState.editing,
title: const Text('Id Proof'),
content: Column(
children: <Widget>[
OutlineButton(
onPressed: chooseImage,
child: Text('Choose Image'),),
showImage(),
OutlineButton(
onPressed: startUpload,
child:Text(Upload Image),
),
Text(status,textAlign:TextAlign.center,style:TextStyle(color:Colors.green))
],
),
),
];
Unable to initialize chooseImage, showImage, startUpload. I have tried to initiate above the steps starting. showing errors
Only static members can be accessed in initializers
chooseImage & startUpload are not functions, if you created a function
void chooseImage() {
// add image picking code here
}
then you need to change
onPressed: chooseImage,
to
onPressed: chooseImage(),
if you didn't make those functions you could also do
onPressed: () {
// add image picking code here
}