flutterflutter-image

Flutter Image object to ImageProvider


I had to read my image source from base64 to flutter Image object.

Image img = Image.memory(base64Decode(BASE64_STRING));

and then i wanted to put the image as a Container background. But DecorationImage is only accepting ImageProvider.

How to convert Image to ImageProvider? or is ther any other way to deliver base64 image to ImageProvider?

Container(
  decoration: BoxDecoration(
    color: Colors.green,
    image: DecorationImage(
      image: img // <-- Expecting ImageProvider
    )
)

Solution

  • Although you can get an ImageProvide from an Image widget using myImageWidget.image, you might as well start with the ImageProvider directly:

    final imageProvider = MemoryImage(bytes);
    

    Image.memory() gives you an Image widget, but MemoryImage gives you the ImageProvider.

    See my fuller answer here for network, asset, and file images.