how to put a variable that i declare inside AssetImage - flutter

For example, i getting a list of restaurant from database and i only want the url of the image to put into the AssetImage but it cant work. I try to use Image.asset but the Imageprovider cannot put Image.asset.
final restaurantlist = _restaurant.restaurant[index];
AssetImage(restaurantlist.imageurl); <--- error
Image.asset(restuarantlist.imagerurl); <--- working fine
any idea why AssetImage cant work?

Image has several constructors.
Image, for obtaining an image from an ImageProvider.
Image.asset, for obtaining an image from an AssetBundle using a
key.
Image.network, for obtaining an image from a URL.
Image.file, for obtaining an image from a File.
Image.memory, for obtaining an image from a Uint8List.
AssetImage class
fetches an image from an AssetBundle,
and Inherits
ImageProvider < AssetBundleImageKey >
Identifies an image without committing to the precise final asset.
TL;DR
When downloading images use:
Image.network(imgUrl)

In flutter, you can show images with Image Widget it takes a property called image which takes any class which extends ImageProvider class which is used to resolve the image you want Since the AssetImage extends ImageProvider it will just resolve your image assets, you will need to provide it to an Image Widget like so:
Image(image: AssetImage(restaurantlist.imageurl));
And the Image.asset is just a shorthand for the above code.

instead of AssetImage use FileImage
DecorationImage(
image: FileImage("your image url") as ImageProvider,
fit: BoxFit.cover,
),
this is my example to have dynamic background for your app
you cans just use FileImage instead of AssetImage

Related

Flutter: how to convert a `StatefulWidget` to an `ImageProvider`

This seems like it should be simple but I cannot seem to find an answer.
When I have a Widget (Stateful or Stateless) that I'm using as an image (for example: SvgPicture, CachedNetworkImage, or FadeInImage) and I need to have an ImageProvider, how can I make that conversion?
For a FadeInImage I can use FadeInImage( image: i, placeholder: p).image.
SvgPicture and CachedNetworkImage do not have a .image property.
Is there a generic way to convert a widget to an ImageProvider ?
I tried ImageProvider( widget ) but ImageProvider is abstract - so another way to ask this question could be:
Is there an instance of ImageProvider that simply takes a widget as a parameter ?
For CachedNetworkImage you can use CachedNetworkImageProvider("YOUR URL HERE")
For FadeInImage you can use AssetImage("") as your Image Provide.
I am not sure about SvgPicture but I'm sure you can find it in the documentation given on Pub.dev

How to convert an Image.file() object to a Image file in flutter

I am displaying an Image with filter effects on Screen using in filter
Image.file(
imageFile,
color: color.withOpacity(0.5),
colorBlendMode: BlendMode.hardLight,
),
I need to write these color effects to 'imagefile'
Image.file(); is a widget, if you need to write widget to a disk as an image.
Take a look at this Creating raw image from Widget or Canvas
otherwise, if you need to edit an image(like adding a filter)
maybe it works
How To Create A Filtered Image In Flutter
or below package
https://pub.dev/packages/photofilters

Flutter 'Flip Card' randomly pick a different picture on each flip

I am creating a Flutter app using the Flip Card Package. I would like to be able to flip a card and see a randomly selected image. Example: Flash cards, one side is a static image, I flip it to see a picture of a dog. I then tap again and it flips to back to the static image. I tap again, and I see a picture of a cat, etc...
I was able to get it all set up. The card flips back and forth but it is not randomly selecting a new card unless I restart the build after each flip. So, I am missing a piece somewhere that tells the app to pick again. Your help would be super appreciated!
Flip Card description https://pub.dev/packages/flip_card
snippet of code
Just as a tip as it seems you are new to SO - please post the code as text so that we can paste is in an IDE and try to run it. As an image its much harder to replicate your issue. Just a tip for going forward. Otherwise, welcome to SO!
So for your issue, I am not certain exactly why since I cannot see the entire context within that snippet BUT i suspect its because the card doesn't know it needs to rebuild since its just being replaced by another image but it doesn't know its a new image.
You can create a Widget class thats stateless and pass in the path. The Key change will ensure that the widget gets rebuilt:
The Widget class:
class FlipCardDetails extends StatelessWidget {
final String imagePath;
const FlipCardDetails({Key key, #required this.imagePath}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
child: Stack(
children: <Widget>[
Image.asset(
this.imagePath,
key: ValueKey<String>(imagePath),
)
],
)
);
}
}
Then in your main code where it says:
back: Container(child: _cardDetails()),
You can replace this with:
back: FlipCardDetails(imagePath: <Provide the image path>);
You can create a key based on the image path itself like so:
key: ValueKey<String>(imagePath)
This all would only work if there is a function you can run when the person clicks the card that would flip the card. That way you can run your randomizer and provide the image path to the widget.
Instead if you can't and the widget needs to calculate its own random path, then make it stateful, and on initState change the image path. That should work as well.
Without more context I can't really be sure if this is right or which solution works better. Let me know and I can to advise more.
I think that the problem is that your calling the 'Random' function only once when initializing your card. Try adding a function in which you call the Random function to change your '_random' and your 'element' and call it every time the card is flipped.You can do that by using the 'onFlipDone' function field of the FlipCard.

What is the difference between an "Image" and "ImageProvider" in Flutter?

There are answers related to this topic but they offer workarounds rather than explanations.
Why can't an Image be used where an ImageProvider is required? Conceptually they sound the same to me.
child: new CircleAvatar(
backgroundImage: NetworkImage("https..."), // works
backgroundImage: Image.asset('images/image.png'), // error
),
The error generated by trying to use an image directly is:
error: The argument type 'Image' can't be assigned to the parameter type 'ImageProvider'.
Image vs ImageProvider
An image provider is what provides the image to an Image widget. ;D
The image provider doesn't necessarily have the image right there but it knows how to get it.
Getting an Image
If you need an Image widget, then use one of these:
Image.asset()
Image.network()
Image.file()
Image.memory()
Getting an ImageProvider
If you need an ImageProvider, then use one of these:
AssetImage()
NetworkImage()
FileImage()
MemoryImage()
Converting an ImageProvider to an Image
If you have an ImageProvider object and you want an Image widget, then do the following:
Image(
image: myImageProvider,
)
Converting an Image to an ImageProvider
If you have an Image widget and you need its ImageProvider, then do the following:
myImageWidget.image
An Image is a widget that displays an image.
The ImageProvider instead allows you to identify an image without knowing exactly where is the final asset. The place of the asset will be resolved later when someone wants to read the image.
Actually Every background and Foreground Decoration will accept only ImageProvider like AssetImage(),NetworkImage(),FileImage(),MemoryImage()...
And when you try to build a widget then its take Image.asset(),Image.network(), Image.file(), Image.memory()...

NetworkImage cannot be assigned to type Widget

I tried to create add an image to my ListView by doing the following
new ListView(
children: <Widget>[
new NetworkImage('my_image_url')
]
)
and got the following error:
The element type 'NetworkImage' can't be assigned to the list type
'Widget'.
NetworkImage isn't a Widget, instead it:
Fetches the given URL from the network, associating it with the given
scale.
Thus, it's used in Widgets like CircleAvatar to provide the source for its image.
The correct way to add an image via a url is to use Image.network('url'):
new ListView(
children: <Widget>[
new Image.network('my_image_url')
]
)
NetworkImage doesn't just:
Fetches the given URL from the network, associating it with the given scale.
It can be used to display an image. In fact you will need it for something called DecoratedBox, which IS a Widget.
Compared to the simple Image.network('my_url'), using DecoratedBox has a few advantages. Because you can alter that image.
You have access to filters, slice, shadows, or even borders.