flutterdartencodingbase64

Invalid character (at character 77) when decoding a base 64 image to show using Image . memory ()


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


Solution

  • 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+'), '')),