uploadFileFromImagePicker method

Future<File> uploadFileFromImagePicker(
  1. XFile file, {
  2. String uploadUrl = '/upload-file',
})

Uploads a file from an XFile object to the specified upload URL.

Parameters:

  • file: The XFile object representing the file to be uploaded.
  • uploadUrl: The URL to which the file will be uploaded. Defaults to /upload-file.

Returns:

  • A Future that resolves to a File object representing the uploaded file.

Implementation

Future<File> uploadFileFromImagePicker(
  XFile file, {
  String uploadUrl = '/upload-file',
}) async {
  FormData formData = FormData();
  if (kIsWeb) {
    final bytes = await file.readAsBytes();
    formData.files.add(MapEntry('file', MultipartFile.fromBytes(bytes)));
  } else {
    formData.files
        .add(MapEntry('file', await MultipartFile.fromFile(file.path)));
  }
  return dio
      .post(uploadUrl, data: formData)
      .then((response) => response.body<File>());
}