Converting base64 to Image in Flutter Error - flutter

I have this image as a base64 String which I then want to convert to image in BoxDecoration like so:
Container(
decoration: BoxDecoration(image: DecorationImage(image: Utility.imageFromBase64String(drink.image))),
Here is the function that decodes the String to Image.
Function
class Utility {
static Image imageFromBase64String(String base64String) {
return Image.memory(
base64Decode(base64String),
fit: BoxFit.fill,
);
}}
However, I am getting an error in BoxDecoration:
The argument type 'Image' can't be assigned to the parameter type 'ImageProvider<Object>'
The error is in this part, I suppose I can't put in Image since it asks for ImageProvider:
image: DecorationImage(image: Utility.imageFromBase64String(drink.image))
Any ideas how to make it work? Thanks!

The image property from DecorationImage is of type ImageProvider<object> not Image.
Return a MemoryImage from imageFromBase64String like so
static MemoryImage imageFromBase64String(String base64String) {
return MemoryImage(
base64Decode(base64String)
);
}
Then this.
image: DecorationImage(
image: Utility.imageFromBase64String(drink.image),
fit: BoxFit.fill,
)

Related

Unable to execute inner statements of a for loop in Dart

I am trying to display an image in my weather app based on the current weather condition.
I have a jsonConditionsDay variable which stores JSON data fetched from https://www.weatherapi.com/docs/conditions.json.
I have a List<String> conditionsDayImgUrls = []; //network image urls contained in this which stores image urls.
Current weather data is fetched using dio from https://www.weatherapi.com/api-explorer.aspx#forecast and stored in a variable jsonData.
Now when I'm calling the following function String getCurrentConditionsBgImgForDay(String str) I am not getting the desired image from the conditionsDayImgUrls list, instead I'm only getting the first image of the list.
Code for getCurrentConditionsBgImgForDay in global_variables.dart:
String getCurrentConditionsBgImgForDay(String str) {
int i;
for (i = 0; i < jsonConditions.length; i++) {
if (str == jsonConditions[i]["day"]) {
return conditionsDayImgUrls[i];
}
}
//print(i);
return conditionsDayImgUrls[0];
}
The following code in current_weather_screen.dart tries to display the desired weather image based on current weather condition:
Container(
width: screenWidth,
height: screenHeight,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
getCurrentConditionsBgImgForDay(
jsonData["current"]
["condition"]["text"])),
fit: BoxFit.fill,
),
),
),
What is wrong in my code that is preventing it from displaying the correct weather image and how to solve it?

How to convert local storage image path into network image url in flutter

I have a local storage image path like:
/Users/newuser/Library/Developer/CoreSimulator/Devices/D7515987-8FDF-47A7-A6E1-A210739CB2A5/data/Containers/Data/Application/DC529635-F924-40DF-B530-85710D23A8D0/Library/Application Support/localstorage_images/2022-05-04/12:50:41.png
I have display this image using
image: FileImage(
url),
fit: BoxFit.cover,
),
here var url contains the local storage image path /Users/newuser/Library/Developer/CoreSimulator/Devices/D7515987-8FDF-47A7-A6E1-A210739CB2A5/data/Containers/Data/Application/DC529635-F924-40DF-B530-85710D23A8D0/Library/Application Support/localstorage_images/2022-05-04/12:50:41.png in File data type
I want to pass this var url in like below:
image: NetworkImage(
url),
fit: BoxFit.cover,
),
my problem was how to convert FileImage type to NetworkImage type because my other images are NetworkImage type it is possible to convert FileImage into Network image in flutter
Thanks in advance.

What is a type of a resulting variable when NetworkImage or FileImage can be reterned?

What is a type of image here?
final image =
imagePath.contains('https://') ? NetworkImage(imagePath) : FileImage(File(imagePath));
usage:
child: Ink.image(
image: image as ImageProvider,
NetworkImage and FileImage are both extending ImageProvider interface.
You can do your cast here :
final ImageProvider image = imagePath.contains('https://') ? NetworkImage(imagePath) : FileImage(File(imagePath)) as ImageProvider
and
child: Ink.image(
image: image,
image type will be NetworkImage or FileImage before cast.

how to show asset image inside box decoration

this is my code using network image inside box decoration.It works completely fine.
Container(
decoration: new BoxDecoration(
borderRadius:BorderRadius.circular(24.0),
image: DecorationImage(
image: new NetworkImage(img),
fit: BoxFit.fill,
)
)
),
)
I want to change images from network to asset image so I can use images from assets folder.
Change new NetworkImage(img) to
Image.asset("path/to/image").image
or
AssetImage("path/to/image")
This will load an image from the assets instead of the internet.

"Unable to load asset" with image picker

I'm using Image Picker package in my Flutter project
I choose image from gallery then preview it in Image.asset widget
The problem here is if image name "example_name.png" (without spaces) the image is visible on the screen, but if image name "example name.png" (with spaces) the image is invisible like this Screenshot.
Error: Unable to load asset: /storage/emulated/0/Download/images (9).jpeg
File _image;
Image.asset(
_image != null
? "${_image.path}"
: getImage("icon.png"),
fit: BoxFit.cover,
width: 120,
height: 120,
);
...
Future chooseFile() async {
await ImagePicker.pickImage(source: ImageSource.gallery).then((image) {
setState(() {
_image = image;
});
});
}
You are using the wrong Image constructor. Use Image.file instead of Image.asset. Image.asset load files packaged in the application (assets section of pubspec.yaml) and ImagePicker does not have access to them.
in the image_picker (version 0.6.7 + 22) I was able to recover the image with this condition
if (photo == null) {
return Image (
image: AssetImage ('assets / no-image.png'),
height: 300.0,
fit: BoxFit.cover,
);
} else {
return Image.file (
Photo,
height: 300.0,
fit: BoxFit.cover,
);
}
Using Image.file is a good option but you like to display it in an effective way use
Image.file(_image).image this will help you to convert Image file to image provider