在Flutter中将BASE64字符串转换为Image

问题描述:

我尝试将 BASE64 字符串解码为 Uint8List

I have tried decoding BASE64 String to Uint8List

Uint8List _bytes =
    base64.decode('data:image/jpeg;base64,/9j/4AAQ .........');
Image.memory(_bytes);

但是却收到错误提示,(字符错误:)

But getting error as, (Error on character :)


无效字符(在字符5)
data:image / jpeg; base64,/ 9j / 4AAQSkZJRgABAQAAAQABAAD / 2wCEAAkGBxITEhUSEhMVFRUV ...

Invalid character (at character 5) data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUSEhMVFRUV...

我怎么摆脱这个问题?

使用URI,其中包含 RFC-2397 定义的逗号后的数据。 Dart的 Uri 类基于RFC-3986 ,因此您将无法使用它。用逗号分隔字符串,并使用字符串的最后一部分:

You're using URI that contains data after 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 comma and take the last part of it:

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