【问题标题】:Display Blob as Image in Flutter在 Flutter 中将 Blob 显示为图像
【发布时间】:2018-01-14 22:19:53
【问题描述】:

有人知道如何使用 Flutter 将 Blob 转换为图像吗?看起来 'dart:html' 库在 Flutter 中不可用。任何帮助表示赞赏。谢谢!

【问题讨论】:

  • 太棒了!请自行回答您的问题。这是允许的,也是分享答案的最佳方式。
  • @SethLadd 谢谢!完毕。 ??????????

标签: image dart blob flutter


【解决方案1】:

如果有人感兴趣,我找到了解决方案:

从 JSON 中获取 blob:

var blob = yourJSONMapHere['yourJSONKeyHere'];

var image = BASE64.decode(blob); // 图像是一个 Uint8List

现在,在Image.memory中使用图像

new Container( child: new Image.memory(image));

这对我有用!

【讨论】:

  • 嗨 Kyle Stokes,可以将普通图像转换为 blob 吗?
  • 如何将图像转换为 blob?
【解决方案2】:

没有找到!!!对于我。

var image = BASE64.decode(blob);

没有找到!!!对于我。

我在 2020 年 - 10 月找到了解决方案:

import 'dart:convert';
import 'dart:typed_data';

从 JSON 中获取 blob:

var blob = yourJSONMapHere['yourJSONKeyHere'];

Uint8List image = Base64Codec().decode(blob); // image is a Uint8List

现在,在 Image.memory 中使用图像

new Container( child: new Image.memory(image));

这对我有用!

【讨论】:

  • json 映射和键是什么意思?有什么例子吗?
【解决方案3】:

自 2021 年 4 月 Flutter 2.0 引入 null 安全性以来,确保您的代码为 null 安全非常重要。这是上面 Andrey Araya 的答案的改进。由于var 不可为空,因此我使用了dynamic 关键字:

// Import dart:convert to help with the decoding of the blob
import 'dart:convert';

dynamic? blob = yourJSONMapHere['yourJSONKeyHere']; // Question mark after the keyword to make it nullable

// Declare variable to save the image later
var image;

if (blob != null) {
   // Only decode if blob is not null to prevent crashes
   image = base64.decode(blob);
}

然后您可以使用Image.memory 小部件呈现您的图像。

Image.memory(image);

【讨论】:

    【解决方案4】:

    E/flutter (8757): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理异常:“Blob”类型不是“String”类型的子类型

    当我们使用“var image=base64.decode(blob);”时会出现这个错误

    【讨论】:

      猜你喜欢
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2013-05-11
      • 1970-01-01
      • 2017-07-28
      • 2017-12-16
      • 2011-06-04
      相关资源
      最近更新 更多