20

I am creating a Flutter project in which, I have a piece of data (JSON) that I want to Import from and Export to a location the user wants to. In order to achieve this, I require a File Picker plugin in Flutter. Now, I searched the Dart Packages repository for "file picker" but didn't find one.

Is there a way to get a File Picker that looks like this:

or even this...

The first screenshot is preferable for me as it allows file selection from different sources (like Drive).

Also, since I want to Export the data, I might want a Folder Picker too. ;)
But, if there is any other alternative to Folder Picker. I'd be happy to know...

Miguel Ruivo
  • 16,035
  • 7
  • 57
  • 87
Melvin Abraham
  • 2,870
  • 5
  • 19
  • 33
  • You'd have to build your own UI, but the [path_provider](https://pub.dartlang.org/packages/path_provider) plugin gives cross-platform access to a couple of directories. Does iOS natively have a screen similar to the first image? You could also create a plugin that uses [platform channels](https://flutter.io/platform-channels/) – Jacob Phillips Jun 22 '18 at 04:00

5 Answers5

31

I've created a file_picker plugin some time ago in order to make it possible to pick (both on iOS and Android) absolute paths and then loaded it with Flutter.

You can check it here: https://pub.dev/packages/file_picker

Kab Agouda
  • 6,309
  • 38
  • 32
Miguel Ruivo
  • 16,035
  • 7
  • 57
  • 87
  • 4
    This is nice plugin. I have a specific requirement to allow picking Image and PDF files only. Is there a way to provide multiple whitelisted file extensions? – Milind Mevada Jun 05 '19 at 06:02
  • @MilindMevada at the moment not. You can't get multiple file extension filter at the same time, unless you use `FileType.IMAGE` or `FileType.VIDEO` or so. In this case yes. However, for other custom types. it makes it hard because I try to keep the interaction between iOS and Android as seamless as possible so the dev doesn't have to be handling himself in the app. – Miguel Ruivo Jun 05 '19 at 10:46
  • Thank you so much for the info. Flutter community really appreciate the open source contributions. – Milind Mevada Jun 05 '19 at 11:56
  • Will there be a Desktop implementation of this plugin? – DirtyNative Jun 29 '19 at 20:50
  • Yes. It’s in the upcoming features, both Desktop and Web. – Miguel Ruivo Jun 30 '19 at 11:27
  • 3
    @MiguelRuivo great work sir ,but it says No implementation found for method any on channel miguelruivo.flutter.plugins.filepicker – Jaydeep chatrola Jun 03 '20 at 06:46
  • A `flutter clean` should probably fix it. – Miguel Ruivo Jun 03 '20 at 14:47
  • @MiguelRuivo Thanks sir for the nice plugin, I used this plugin once ago, and it was working perfectly, but now it show this message the method 'getFile' isn't defined for the type 'FilePicker'. – Naham Al-Zuhairi Feb 23 '21 at 06:55
  • @NahamAl-Zuhairi you've probably updated the plugin to a later version. You've probably need to use it now as `FilePicker.platform.pickFiles()`. Check it [here](https://pub.dev/packages/file_picker). – Miguel Ruivo Feb 25 '21 at 15:23
  • Does Flutter really provide NO builtin file chooser dialog? Can't believe. Created a MacOS app - but can's save file generated stuff? – SteAp Apr 19 '22 at 21:18
  • Note, that Flutter.dev team's FileSelector (see answer above) depends on nothing. – SteAp Apr 19 '22 at 22:36
4

I used file_picker library to pick files. you can use this for pick images as well.

Future getPdfAndUpload(int position) async {

    File file = await FilePicker.getFile(
      type: FileType.custom,
      allowedExtensions: ['pdf','docx'], //here you can add any of extention what you need to pick
    );

    if(file != null) {

      setState(() {

          file1 = file; //file1 is a global variable which i created
     
      });

    }
  }

here file_picker flutter library.

Supun Dewapriya
  • 695
  • 11
  • 13
0

I'm in the exact same boat as you, haha!

I noticed documents_picker 0.0.2. It allows the user to pick multiple files, and it seems to fit the need!

check it out: https://pub.dartlang.org/packages/documents_picker#-readme-tab-

Kael Kirk
  • 324
  • 2
  • 9
0

Here's a better document picker. It looks like the native document picker from the Storage Access Framework, which is what you have in your picture. flutter_document_picker

ThinkDigital
  • 3,189
  • 4
  • 26
  • 34
0

Just found the FileSelector plugin from flutter.dev. Compatible with MacOS, Windows and Web.

From its pub.dev page:

Open a single file

final typeGroup = XTypeGroup(label: 'images', extensions: ['jpg', 'png']);
final file = await openFile(acceptedTypeGroups: [typeGroup]);

Open multiple files at once

final typeGroup = XTypeGroup(label: 'images', extensions: ['jpg', 'png']);
final files = await openFiles(acceptedTypeGroups: [typeGroup]);

Saving a file

final path = await getSavePath();
final name = "hello_file_selector.txt";
final data = Uint8List.fromList("Hello World!".codeUnits);
final mimeType = "text/plain";
final file = XFile.fromData(data, name: name, mimeType: mimeType);
await file.saveTo(path);

MacOS: Provide file read or/and write privileges

On target MacOS please provide sufficient rights using Xcode:

Set file read/write privileges using Xcode

In case you don't provide file read or/and write permissions, the call to

final XFile? file =
    await openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]);

neither shows anything not returns.

SteAp
  • 11,853
  • 10
  • 53
  • 88