Flutter:将Base64 String image url 转成File 并在FileImage 或相关widgets 中使用

Flutter: Convert Base64 String image url to File and use it in FileImage or related widgets

这个方法我试过了,用在了FileImage但是没找到集结点

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

imageBase64是编码后的图像url。

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

谁能帮帮我。

如果您在获得字节后想做的是显示图像,那么您可以使用 Image.memory

var imageWidget = Image.memory(bytes);

OTOH 如果您尝试上传图片,则无需将其转换为文件。假设你不能使用 base64 编码上传它,你可以直接使用字节。包裹 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();

如果你有一个 UintList8,你可以使用 MemoryImage(bytes)。

Uint8List bytes = base64.decode(imageBase64);
Image(image: MemoryImage(bytes));