Search code examples
flutterimagedartbase64converters

Convert image file into base64 image in Flutter Web


I am using the flutter camera plugin to take pictures from my app using the camera device and send them as base64 images on the server.

When I want to convert the generated image into a base 64 image I can perfectly do it from my android emulator with this line :

base64img = base64Encode(File(image.path).readAsBytesSync());

"image" is a XFile. "image.path" is a String.

When I try to use the same line on the web I get this error :

Exception caught by gesture ═══════════════════════════════════════════ The following UnsupportedError was thrown while handling a gesture: Unsupported operation: _Namespace

I tried with this line (directly trying to convert the XFile instead of converting a generated File from the image path String) :

base64img = base64Encode(image.readAsBytesSync())

I get the same error.

Thanks for helping.


Solution

  • Found the solution, using readAsBytes instead of readAsBytesSync :

    var bytes = await widget.image.readAsBytes();
    var base64img = base64Encode(bytes);