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'.
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.
If you need an Image
widget, then use one of these:
Image.asset()
Image.network()
Image.file()
Image.memory()
If you need an ImageProvider
, then use one of these:
AssetImage()
NetworkImage()
FileImage()
MemoryImage()
If you have an ImageProvider
object and you want an Image
widget, then do the following:
Image(
image: myImageProvider,
)
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()...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With