How to retrieve an image in flutter from flask api How to retrieve an image in flutter from flask api flask flask

How to retrieve an image in flutter from flask api


You can convert the image to base64 and display it with Flutter.

On server:

import base64...data_object["img"] = base64.b64encode(b.read()).decode('ascii')...

On client:

...String imageStr = json.decode(response.data)["img"].toString();Image.memory(base64Decode(imageStr));...

The problem with your server-side code is it tries to coerce a bytes to str object by using function str().

However, in Python 3, bytes.__repr__ is called by str() since bytes.__str__ is not defined. This results in something like this:

str(b'\xf9\xf3') == "b'\\xf9\\xf3'"

It makes the JSON response looks like:

{"img": "b'\\xf9\\xf3'"}

Without writing a dedicated parser, you can not read this format of image data in Flutter. However, base64 is a well known format of encoding binary data and we do have a parser base64Decode in Flutter.