Cyrus the Great
Cyrus the Great

Reputation: 5932

Flutter: Base64 convert FormatException

I am trying to convert base64 image into Image on flutter and shows in list view like below:

  _fileItem(Model file) {
    return Card(
      child: ListTile(
        leading: Image.memory(
          base64Decode(file.image),
          width: 100,
          fit: BoxFit.cover,
        ),
      ),
    );
  }

but i got this error:

The following FormatException was thrown building:
Invalid character (at character 77)
iVBORw0KGgoAAAANSUhEUgAAAUAAAAFACAIAAABC8jL9AAAAA3NCSVQICAjb4U/gAAAgAElEQVR4
                                                                            ^

to testing my base64 image by online converter site i opened my base64 string file and this is results:

enter image description here

But in flutter i got error?

I converted this base64 in java native in my plugin by this method:

private String getBase64Image(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

Upvotes: 0

Views: 1061

Answers (1)

Vilsad P P
Vilsad P P

Reputation: 1559

your base64 encoded string contains line breaks. may be because you copied it from text editor. remove the breaks and it should work.

you can check the string here https://www.textmagic.com/free-tools/unicode-detector and it will display carriage returns ('\n').

Upvotes: 1

Related Questions