savke
savke

Reputation: 93

Flutter folder picker

I have a button on screen, when you click button I need to open some folder picker. I have searched for package, but I didn't found any package that also support Android and iOS. Does someone knows some package who will resolve this problem, or something I could use to make solution of picking folder on iOS?

EDIT:

I found way to get directory picker functionality,I didn't test on iOS but in documentation says it works on both (Android and iOS).

String path = await FilePicker.platform.getDirectoryPath();

With this line above you get new screen to select your directory and get a path like result from that Future.

This is solved using file_picker package.

Upvotes: 8

Views: 6252

Answers (2)

Naveenchandar
Naveenchandar

Reputation: 1

In Flutter web, selecting a folder is not straightforward because the default file_picker package does not support folder selection out of the box. However, you can achieve this using the universal_html package, "webkitdirectory" which allows you to access lower-level HTML APIs. Here’s how you can implement a folder picker using universal_html:

_pickDirectory

  void _pickDirectory() {
try {
  var responseBody= jsonEncode(<String, dynamic>{
    "flag":0
  });

  html.FileUploadInputElement uploadInput = html.FileUploadInputElement();
  uploadInput.accept = '*/*'; // Accept all file types
  uploadInput.multiple = true; // Allow multiple file selection
  uploadInput.attributes['webkitdirectory'] = ''; // Enable directory selection on webkit browsers
  uploadInput.click(); // Simulate a click to open file picker
  uploadInput.onChange.listen((e) async {
    final files = uploadInput.files;
    if (files != null && files.isNotEmpty) {
      await FolderPicService().sendDirectoryToBackend(files, responseBody).then((value) {
        if (value != null) {
          _zipfileUrl = value['zipfile_url'].toString();
          _eventId = int.parse(value['eventId'].toString());
          setState(() {

          });
        }
        Navigator.of(context).pop();
      });
    }
  });
} catch (e) {
  print(e);
}
}

Upvotes: 0

Hylke Reinders
Hylke Reinders

Reputation: 91

Use the package file_picker.

Easy to use:

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

if(result != null) {
   File file = File(result.files.single.path);
} else {
   // Do something else...
}

Upvotes: 8

Related Questions