Miko
Miko

Reputation: 862

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

I've tried this way, and use it in FileImage but haven't found a rally point

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

imageBase64 is the image url that has been encode.

The point of this thing is, i want to use it in FileImage or another widget image file but i don't want this image is saved to device and upload this file to the attachments file Rest API

can anyone help me.

Upvotes: 0

Views: 2403

Answers (2)

Claudio Redi
Claudio Redi

Reputation: 68400

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();

Upvotes: 1

JRamos29
JRamos29

Reputation: 890

If you have an UintList8 you can use MemoryImage(bytes).

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

Upvotes: 0

Related Questions