flutter_file_picker icon indicating copy to clipboard operation
flutter_file_picker copied to clipboard

[Suggestion] Interop between PlatformFile and XFile

Open navaronbracke opened this issue 3 years ago • 13 comments

The plugin uses PlatformFile to provide a cross-platform File implementation. This is fine by itself. However official Flutter plugins use cross_file. For example: a pubspec contains both file_picker and camera. Now there are 2 different cross platform file implementations that someone could use.

Why don't we migrate to using XFile instead? This also eliminates the need for having to convert between the two. I.e. the result of CameraController.stopRecording() doesn't have to be converted to a PlatformFile after recording, which in itself involves another Future for the size & bytes.

navaronbracke avatar Nov 16 '21 10:11 navaronbracke

This issue is stale because it has been open for 14 days with no activity.

github-actions[bot] avatar Nov 27 '21 01:11 github-actions[bot]

cc @miguelpruivo

navaronbracke avatar Nov 27 '21 09:11 navaronbracke

What would be the implications in the file picker for this?

miguelpruivo avatar Nov 27 '21 16:11 miguelpruivo

What would be the implications in the file picker for this?

A) return an XFile from the file picker methods, instead of PlatformFile's. Since PlatformFile and XFile are both wrappers around dart:io's File class, this seems doable? The only thing I don't exactly know is how we would keep track of the new identifier attribute? Perhaps let PlatformFile extend XFile? Or provide a method like PlatformFile.fromXFile(file) ?

B) updating the documentation on how you could create an XFile from a PlatformFile and the other way around (from XFile to PlatformFile). (perhaps give the camera plugin as a usage example)

navaronbracke avatar Nov 27 '21 17:11 navaronbracke

The PlatformFile contains multiple handy features as well as being able to abstract Web implementation which doesn’t support IO imports.

Anyway, I think I can add a way to retrieve a XFile directly from the PlatformFile and that can be a plus for some.

miguelpruivo avatar Nov 27 '21 18:11 miguelpruivo

This issue is stale because it has been open for 14 days with no activity.

github-actions[bot] avatar Dec 05 '21 01:12 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Dec 19 '21 01:12 github-actions[bot]

It would be great if we could retrieve a XFile from file_picker because it makes it easier to use this package in combination with other packages.

philitell avatar May 19 '22 11:05 philitell

This could make sense indeed. I'll consider it.

miguelpruivo avatar May 20 '22 14:05 miguelpruivo

Flutter WEB: I am using both packages in one widget. Any news?

Patrick386 avatar Nov 20 '22 15:11 Patrick386

Any update or workaround - I am using XFile extensively and would like to use this package. Any way to convert PlatformFile to XFile?

sunilguptasg avatar Apr 13 '23 06:04 sunilguptasg

I use following code convert PlatformFile to XFile with withData:true

var result = await FilePicker.platform.pickFiles();
if (kIsWeb) {
    return result?.files.map((f) => XFile.fromData(f.bytes!, name: f.name, length: f.size)).toList()??[];;
} else {
  return result?.files.map((f) => XFile(f.path!, name: f.name, bytes: f.bytes, length: f.size)).toList()??[];;
}

LaysDragon avatar Sep 19 '23 23:09 LaysDragon

I think file_picker should optionally not copy the file to a temporary path if the app has MANAGE_EXTERNAL_STORAGE permission. Of course, this is an edge case but this should be handled by the plugin.

Tienisto avatar Dec 10 '23 02:12 Tienisto

Added in the last version (7.1.0) you can now retrieve the files as xFiles or individually with xFile getter.

Thanks!

miguelpruivo avatar Mar 20 '24 11:03 miguelpruivo