1

so i was trying to show an image using a base64 string in flutter using the following code:

class GeneratedImages extends StatelessWidget {
      const GeneratedImages({
        Key? key,
       required this.imageBytes,
   }) : super(key: key);
     final String imageBytes;

    @override
          Widget build(BuildContext context) {
return Image.memory(
   const Base64Decoder().convert(imageBytes),
  errorBuilder: (_, object, stackTrace) {
    return const Center(
      child: Icon(Icons.image_not_supported),
    );
  },
);
}
}

When i run the code, the following error is shown

 FormatException: Invalid character (at character 77)

The Base 64 string which i have is : /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEP ERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4e Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCAEAAQA FK5uYR9j05ISHI/eMTjPTGCPbtWJqHi3xJcyBhew27548oKMDPAG7k9cV1UsJWTu3YyqYml6n//Z

The Full base64 string has been uploaded on this link (free to view and edit)

Any Help Will Be Appreciated

austin
  • 517
  • 5
  • 16
  • 1
    The full error message should indicate the invalid character: a space. Whitespace is not accepted by Dart's base64 decoder. If you can't correct the base64 string at the source, you will need to remove the whitespace first (e.g. `base64.decode(imageBytes.replaceAll(RegExp(r'\s'), ''))`). – jamesdlin Jul 24 '22 at 07:40
  • @jamesdlin i have uploaded the whole string on https://textdoc.co/319QZh7rEgdGbt26 and there doesn't seem to be any whitespace in it. – austin Jul 24 '22 at 07:42
  • The string there is split across multiple lines. You will need to remove the newline characters. I've updated my comment. – jamesdlin Jul 24 '22 at 07:43
  • i removed newline characters but i haven't found any whitespace at 77 either – austin Jul 24 '22 at 07:47
  • I can decode your string fine after using `.replaceAll(RegExp(r'\s', '')` on it. If you still can't get it to work, post a minimal, complete code example that reproduces the problem. – jamesdlin Jul 24 '22 at 07:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/246707/discussion-between-austin-and-jamesdlin). – austin Jul 24 '22 at 07:59

2 Answers2

3

Based on the answer suggested in the comments by @Jamesdlin .

Removing all whitespaces present in the string will solve the problem .

Was able to remove whitespaces by using this :

 base64.decode(imageBytes.replaceAll(RegExp(r'\s+'), '')),
austin
  • 517
  • 5
  • 16
  • I'd recomment using `r'\s+'` as the regexp. Just because it handles `\r\n` more efficiently. – lrn Jul 25 '22 at 09:55
0

If your Base64 string contains data after the comma as it is defined by RFC-2397. Dart's Uri class is based on RFC-3986, so you can't use it.

Split the string by a comma and take the last part of it:

String imgbase64 = 'data:image/gif;base64,...';
Uint8List _bytes = base64.decode(imgbase64.split(',').last);

REFERENCE: https://stackoverflow.com/a/59015116/12382178