Flutter Base64 Encoded String Is Incomplete

Viewed 3021

I'm trying to convert an image byte to base64 encoded string, this operation is successfull but when i print out the encoded string i get the ff:

/9j/4RgvRXhpZgAASUkqAAgAAAANAAABBAABAAAAQAYAAAEBBAABAAAAsAQAAA8BAgAIAAAAqgAAABABAgAIAAAAsgAAABIBAwABAAAACAAAABoBBQABAAAAugAAABsBBQABAAAAwgAAACgBAwABAAAAAgAAADEBAgANAAAAygAAADIBAgAUAAAA2AAAABMCAwABAAAAAQAAAGmHBAABAAAA7AAAACWIBAABAAAA7gIAAAADAABzYW1zdW5nAFNNLVA1NTUASAAAAAEAAABIAAAAAQAAAFA1NTVYWFUxQ1JBMgAAMjAxOTowMToxNCAwOTozMzoyOAAZAJqCBQABAAAAHgIAAJ2CBQABAAAAJgIAACKIAwABAAAAAgAAACeIAwABAAAAZAAAAACQBwAEAAAAMDIyMAOQAgAUAAAALgIAAASQAgAUAAAAQgIAAAGRBwAEAAAAAQIDAAKSBQABAAAAVgIAAAWSBQABAAAAXgIAAAeSAwABAAAAAgAAAAiSAwABAAAAAAAAAAmSAwABAAAAAAAAAAqSBQABAAAAZgIAAHySBwBiAAAAbgIAAACgBwAEAAAAMDEwMAGgAwABAAAAAQAAAAKgBAABAAAAQAYAAAOgBAABAAAAsAQAAAWgBAABAAAA0AIAABeiAwABAAAAAgAAAAGjBwABAAAAAQAAAAKkAwABAAAAAAAAAAOkAwABAAAAAAAAAAakAwABAAAAAAAAAAAAAAABAAAAFAAAAPAAAABkAAAAMjAxOTowMToxNCAwOTozMzoyOAAyMDE5OjAxOjE0IDA5OjMzOjI4AP0AAABkAAAA/QAAAGQAAADrAAAAZAAAAAcAAQAHAAQAAAAwMTAwA

An encoded string is supposed to be way more than the characters above. Now when i try to view this by appending data:image/jpeg;base64, to the begin of the encoded string, nothing gets displayed.

Is flutter cutting the string in the console or what and if yes then how do i get the complete encoded string.

5 Answers

You should convert it to Bytes if you're trying to send it to the backend. This will give you the long string you're looking for ;)

var params = {
      "image_file": base64Encode(_selectedImage.readAsBytesSync()),
    };

For me base64 is not showing completely. if that the case, print() and debugPrint() both are used for logging in the console. If you are use print() and output is too much at once, then Android sometimes discards some log lines. To avoid this, use debugPrint().

Probably the debug output windows is truncating it since it's too long. Please make sure on API side whether you take it completely or not.

here is my code

static Future<Response> upload64(File file) 
    List<int> imageBytes = file.readAsBytesSync();
    String base64Image = base64Encode(imageBytes);

    String fileName = path.basename(file.path);

    var body = {"fileName": fileName, "base64": base64Image};
    print("http.upload >> " + body.toString());

  }
Related