Make PlatformFile into File in Flutter using File Picker
Asked Answered
T

4

14

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;
    }
  }
Thrombus answered 12/12, 2020 at 22:7 Comment(0)
C
17

Try this:

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

Happy coding! :)

Cartesian answered 18/1, 2021 at 10:0 Comment(6)
This doesn't work on Flutter Web as PlatformFile.path is not available on web.Brachiopod
@Brachiopod - maybe, I've used it only on mobile devices (Androis and IOS).Cartesian
@RobertRobinson It works on web, you have to add the path, name and size I think.Idolum
it will not work with webFarmann
Will not work on web. And also PlatformFile.path returns a String while File() expects a PathReeding
How can I do it on Flutter web?Disequilibrium
S
4

If you're on a web app, you can post image files to Firestore with flutter_file_picker: (Taken from the FAQ page): https://github.com/miguelpruivo/flutter_file_picker/wiki/FAQ

// get file
final result = await FilePicker.platform.pickFiles(type: FileType.any, allowMultiple: 
false);

if (result.files.first != null){
  var fileBytes = result.files.first.bytes;
  var fileName = result.files.first.name;

  // upload file
  await FirebaseStorage.instance.ref('uploads/$fileName').putData(fileBytes);
}
Subtraction answered 13/5, 2021 at 20:18 Comment(1)
This should be added to the accepted answer to have a complete answerWoody
B
1

Based on the documentation if you have multiple files you can do:

FilePickerResult? result = await FilePicker.platform.pickFiles(allowMultiple: true);

if (result != null) {
  List<File> files = result.paths.map((path) => File(path)).toList();
} else {
  // User canceled the picker
}

NB: I am not sure that it would work with Flutter Web. It should work with all the other platforms.

Bobbysoxer answered 23/10, 2023 at 17:33 Comment(0)
P
-2

This works

File(platformFile.name)

Just be sure not duplicates in the file names in your logic.

Plausive answered 5/12, 2022 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.