【问题标题】:Can't transform Blob coming from a JSON into file无法将来自 JSON 的 Blob 转换为文件
【发布时间】:2021-08-10 05:37:23
【问题描述】:

我在 mySQL 上保存了一个 base64 编码的图像。当我使用我的 API 获取图像时,它返回一个 Blob:

Blob on Json

但是当我尝试获取 Blob 时,我收到错误消息:

'_InternalLinkedHashMap' 不是“String”类型的子类型

该错误发生在 base64.decode(pictureBlob) 行,因此 Blob 是一个列表。但是我怎样才能正确获得 Blob?

我用来获取 Blob 的代码:

  Future<Profile> getProfile(ModelUser user) async {
_headers['Authorization'] = "Bearer ${user.token}";

final response = await get(
    Uri.http(_options.baseUrl, '/persons/${user.id}'),
    headers: _headers);

if (response.statusCode == 200) {
  print("ui");
  Map<String, dynamic> profileMap = jsonDecode(response.body);
  var pictureBlob = profileMap['picture'];

  print(pictureBlob);
  print(base64.decode(pictureBlob));

  return Profile.fromJson(profileMap);
}

【问题讨论】:

    标签: mysql flutter dart sequelize.js blob


    【解决方案1】:

    看看你的json。通过var pictureBlob = profileMap['picture'];这条线你会得到一张地图

    {
      "type" : "Buffer",
      "data" : [47, 57, ......]
    }
    

    所以要获取你需要使用的blob文件

    //getting the blob from json
    var pictureBlob = profileMap['picture']['data'];
    //convert it to Uint8List
    var image = base64.decode(pictureBlob);
    

    然后使用图片作为Image.memory(image);

    【讨论】:

    • 是的,但是我该如何使用它呢?实际上我正在将它转换为 Uint8List,但他的值在 base64 中,所以我需要在加载图像之前读取它并解码。 Buuut,我怎么能把这个 blob 读回我的 base64 字符串?
    【解决方案2】:

    我已经弄清楚如何让它在我的代码上运行。 Tipu Saltan 的回答很好但还不够:我的节点正在以字节形式检索图像。

    好的,我正在发送以 base 64 编码的图片。这没有错。 但是要将接收到的字节转换为图像,我需要以下代码:

        List<int> pictureData = profileMap['picture']['data'].cast<int>();
        Uint8List pictureBytes = Uint8List.fromList(pictureData);
        String pictureBase64 = new String.fromCharCodes(pictureBytes);
    
        profileMap['picture'] = base64Decode(pictureBase64);
    

    感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-17
      • 2021-06-20
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 2020-04-22
      相关资源
      最近更新 更多