【发布时间】:2018-01-14 22:19:53
【问题描述】:
有人知道如何使用 Flutter 将 Blob 转换为图像吗?看起来 'dart:html' 库在 Flutter 中不可用。任何帮助表示赞赏。谢谢!
【问题讨论】:
-
太棒了!请自行回答您的问题。这是允许的,也是分享答案的最佳方式。
-
@SethLadd 谢谢!完毕。 ??????????
有人知道如何使用 Flutter 将 Blob 转换为图像吗?看起来 'dart:html' 库在 Flutter 中不可用。任何帮助表示赞赏。谢谢!
【问题讨论】:
如果有人感兴趣,我找到了解决方案:
从 JSON 中获取 blob:
var blob = yourJSONMapHere['yourJSONKeyHere'];
var image = BASE64.decode(blob); // 图像是一个 Uint8List
现在,在Image.memory中使用图像
new Container( child: new Image.memory(image));
这对我有用!
【讨论】:
没有找到!!!对于我。
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));
这对我有用!
【讨论】:
自 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);
【讨论】:
E/flutter (8757): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理异常:“Blob”类型不是“String”类型的子类型
当我们使用“var image=base64.decode(blob);”时会出现这个错误
【讨论】: