Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make PlatformFile into File in Flutter using File Picker

I am using the File Picker Plugin to choose a file from a device. The file is chosen in the datatype of a PlatformFile, but I want to send the file to Firebase Storage and I need a regular File for that. How can I convert the PlatformFile into a File so that I can send it to Firebase Storage? Here is the code:

PlatformFile pdf;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

void _trySubmit() async {
    final isValid = _formKey.currentState.validate();
    if (isValid) {
      _formKey.currentState.save();
      final ref = FirebaseStorage.instance
          .ref()
          .child('article_pdf')
          .child(title + '-' + author + '.pdf');
      await ref.putFile(pdf).onComplete; // This throws an error saying that The argument type 'PlatformFile' can't be assigned to the parameter type 'File'
    }
  }

void _pickFile() async {
    FilePickerResult result = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['pdf'],
    );
    if (result != null) {
      pdf = result.files.first;
    }
  }
like image 980
Shitij Govil Avatar asked Dec 12 '20 22:12

Shitij Govil


People also ask

How do I convert PlatformFile to flutter?

Try this: PlatformFile pdf; final File fileForFirebase = File(pdf. path);

How do I use file picker in flutter?

For Single file: If you want to pick a single file then no need to pass allowMultiple, default value is false for allowMultiple. For Multiple file: If you want to pick more then one file at once(multiple file picking), you need to use below code. 2. allowedExtensions: : Allow only specific extensions file to be picker.

How do you attach a file in flutter?

You can add a file attachment to a PDF document using the PdfAttachment class. The following code example shows this. //Creates a new PDF document PdfDocument document = PdfDocument(); //Create and add attachment to the PDF document document. attachments.


1 Answers

Try this:

PlatformFile pdf;
final File fileForFirebase = File(pdf.path);

Happy coding! :)

like image 83
Max Mit Avatar answered Sep 18 '22 00:09

Max Mit