how to convert an image to base64 image in flutter?

前端 未结 2 1565
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 09:35

I am actually trying to convert an image picked by ImagePicker in flutter to base64 image. I am always getting the error.

FileSyst         


        
相关标签:
2条回答
  • 2020-12-01 10:04

    hi I just changed my code as follows,

    List<int> imageBytes = widget.fileData.readAsBytesSync();
    print(imageBytes);
    String base64Image = base64Encode(imageBytes);
    

    and this is working fine now.

    It is better to read it asynchronously as the image can be very large which may cause blocking of main thread

     List<int> imageBytes = await widget.fileData.readAsBytes();
    
    0 讨论(0)
  • 2020-12-01 10:05

    you can simply change the image to string :

    final bytes = Io.File(imageBytes.path).readAsBytesSync();
    
    String img64 = base64Encode(bytes);
    
    0 讨论(0)
提交回复
热议问题