首页 > 解决方案 > 将 Uint8List 转换为文件

问题描述

我正在使用运行良好的图像选择器网络。我可以在 中显示图像Image.memory(),但此图像以 Uintlist8 格式显示。为了保存在存储中需要格式File,我的问题是如何在Firebase Storage中保存图像。

网络图像选择器:

class _SecondPageState extends State<SecondPage> {
  final _formkey = GlobalKey<FormState>();
  Uint8List _image;

  getImage() async {
    Uint8List tempImg = await ImagePickerWeb.getImage(asUint8List: true);
    if (tempImg != null) {
      setState(() {
      _image = tempImg;
    });
  }
}

标签: flutterdartflutter-web

解决方案


请试试 ....

final _formkey = GlobalKey<FormState>();
  Uint8List _image;
 getImage() async {
 Uint8List tempImg = await ImagePickerWeb.getImage(asUint8List: true);
 if (tempImg != null) {
  setState(() {
  _image = tempImg;
  final tempDir = await getTemporaryDirectory();
  final file = await new File('${tempDir.path}/image.jpg').create();
  file.writeAsBytesSync(_image);
  });
 }
}

推荐阅读