0

I have a base64 string given by an external api. The api send that json data that has the base64 image as string. How can i use this string to display an image using Image.memory() in flutter

Example of the base64 string that the api sends:

iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAEAAElEQVR4nLz9+bNt23Ueho1mzrnW2nuf9vb3vhbvASDwAAKgRJEUJVak2GUroiIlllV2JFVFVS7/YDn+F1SRo8ofILsSxxXHicMoLpOynbJE0aRIkQQbUBIoAO+hff297/b39HuvteacY4z8MNba9wAEGVkksotFvHvOPrtZa8zRfOMb30CzAYAAEADUVFRVlUNgRAAwAACotZ6cHvd9n3MuYwaApm0Wy+X9+/fPzs9zKV/+Z7/71te+/u67H35072EpypwMAVQQIYRgpojIHJjZzMwsxRDbhETjpi+l1iIqYmBipiLEHDkEZpXctc1q2TETYI1su4t068r+1f2dRRfNjJnbtm2bJ
KJS
  • 1
  • 1
  • Maybe this will help you https://stackoverflow.com/questions/59015053/base64-string-to-image-in-flutter – Er1 Aug 04 '20 at 14:29
  • Actually the image does not work in the continer using the DecorationImage()!!!!. I removed it and it worked charmlessly – KJS Aug 04 '20 at 14:35

1 Answers1

0

You can try this code:

var image;
    Image.memory(result.bodyBytes).image;
    
    image == null ? Center(
                        child: CircularProgressIndicator(),
                    ) : Column(
                        children: <Widget>[
                            Container(
                                width: MediaQuery.of(context).size.width,
                                height: MediaQuery.of(context).size.height * 0.51,
                                decoration: BoxDecoration(
                                    image: new DecorationImage(
                                        fit: BoxFit.contain, image: image),
                                ),
                            ),
Captivity
  • 279
  • 5
  • 21
  • The problem i am facing is that the datatype of the base64 data that i am getting is of type String and thus there are no bytes to put directly into the `Image.memory()` – KJS Aug 04 '20 at 14:30
  • And if you replace `Image.memory(result.bodyBytes).image;` to `Image.memory(YOUR_STRING).image;` ? – Captivity Aug 04 '20 at 14:39
  • no it does not work with container if i use the `Image.memory()` in the body directly it works with the second answer that you gave and thankyou for you input it was very helpful – KJS Aug 04 '20 at 15:05
  • @KJS so perfectly, I',m happy. – Captivity Aug 05 '20 at 10:46