How to convert Uint8List image to File Image for upload in flutter web How to convert Uint8List image to File Image for upload in flutter web dart dart

How to convert Uint8List image to File Image for upload in flutter web


File.fromRawPath(Uint8List uint8List);


I tried to generate one code who can support both device and web together.

Because File.fromRawPath() uses dart:io and its not working for web.

This is my solution :

Uint8List imageCroppedBytes;

First, I picked my image by image_picker then cropped by extended_image .

Somewhere in my code after cropping I encoded cropped byte file to jpg.

imageCroppedBytes = Image.encodeJpg(src , quality: 80);

Then :

var image = http.MultipartFile.fromBytes('image', imageCroppedBytes ,filename: 'profileImage.jpg');request.files.add(image);await request.send().then((value) async {    if(value.statusCode == 200) {      Do Something ...    }});

In my case I have a NodeJs back with Multer to get the file and save it.

Editted :

import 'package:image/image.dart' as Image;

More code for help :

var data = editorKey.currentState.rawImageData;Image.Image src = Image.decodeImage(data);src = Image.copyCrop(src, cropRect.left.toInt(), cropRect.top.toInt(),                          cropRect.width.toInt(), cropRect.height.toInt());if(src.width > 300) {src = Image.copyResize(src,width: 300 , height: 300);}setState(() {   imageCroppedBytes = Image.encodeJpg(src , quality: 80);   imagePicked = false;   imageCropped = true;});