Make PlatformFile into File in Flutter using File Picker

2,975

Try this:

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

Happy coding! :)

Share:
2,975
Shitij Govil
Author by

Shitij Govil

Updated on December 26, 2022

Comments

  • Shitij Govil
    Shitij Govil over 1 year

    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;
        }
      }