I'm trying to create a File object with the method File.fromUri(Uri.parse(uploadedImage)); where the input uploadedImage is an FFUploadedFile data type with data that comes from an image the user uploads. I'm trying to use the code below:

File file = File.fromUri(Uri.parse(uploadedImage));
final bytes = await file.readAsBytes();
final archiveFile = ArchiveFile('uploadedImages', bytes.length, bytes);

And uploaded image comes in the format: FFUploadedFile(name: 1700600326427000_0.png, bytes: 239701, height: null, width: null, blurHash: null,)

However, when I try to run the above Custom Code, I get the error message: "The argument type 'FFUploadedFile' can't be assigned to the parameter type 'String'"

How do I get File.fromUri to work with the FFUploadedFile data type as the input?

1

There are 1 best solutions below

0
On

The error you're encountering indicates a type mismatch. The File.fromUri method expects a String as input, which represents the URI of the file. However, you're passing an FFUploadedFile object directly.

To resolve this, you'll need to use the properties of the FFUploadedFile object to retrieve the necessary information, such as the bytes or the file path, and then create a File object.

Look at bellow code segment,

// Assuming 'uploadedImage' is an instance of FFUploadedFile
Uint8List bytes = uploadedImage.bytes; // Retrieve bytes from FFUploadedFile

// Create a temporary file using the bytes
File tempFile = await _createTemporaryFile(bytes, uploadedImage.name);

// Function to create a temporary file from bytes
Future<File> _createTemporaryFile(Uint8List bytes, String fileName) async {
  Directory tempDir = await getTemporaryDirectory();
  String tempPath = tempDir.path + '/' + fileName;
  File tempFile = File(tempPath);
  await tempFile.writeAsBytes(bytes);
  return tempFile;
}

// Now you can proceed to use the 'tempFile' as needed
final bytesFromFile = await tempFile.readAsBytes();
final archiveFile = ArchiveFile('uploadedImages', bytesFromFile.length, bytesFromFile);

This code assumes you have access to the FFUploadedFile properties such as bytes and name to retrieve the necessary information to create a temporary file using File. Then, you can perform operations on this temporary file as required.