首页 > 解决方案 > Flutter:将 Base64 String 图像 url 转换为 File 并在 FileImage 或相关小部件中使用

问题描述

我已经尝试过这种方式,并使用它,FileImage但还没有找到一个集结点

Uint8List bytes = base64.decode(imageBase64);
File file = File.fromRawPath(bytes);

imageBase64是已经编码的图片url。

这件事的重点是,我想在FileImage或其他小部件图像文件中使用它,但我不希望将此图像保存到设备并将此文件上传到附件文件 Rest API

谁能帮我。

标签: flutterdartbase64

解决方案


If what you want to do once you have the bytes is to show the image, then you can use Image.memory.

var imageWidget = Image.memory(bytes);

OTOH If you're trying to upload the image, you don't need to convert it into file. You can use bytes directly assuming you can't upload it using base64 enconding. Package http

import 'package:http/http.dart';

...

//create multipart using filepath, string or bytes
MultipartFile multipartFile = MultipartFile.fromBytes(
  'file',
  byteData,
  filename: fileName,
);

//add multipart to request
request.files.add(multipartFile);
var response = await request.send();

推荐阅读