在Flutter中将BASE64字符串转换为Image [英] BASE64 string to Image in Flutter

查看:853
本文介绍了在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);

这篇关于在Flutter中将BASE64字符串转换为Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆