Flutter folder picker
Asked Answered
D

2

7

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.

Danelle answered 3/8, 2021 at 9:53 Comment(0)
H
8

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...
}
Herbst answered 3/8, 2021 at 10:34 Comment(2)
But can you only pick directories, and not showing files?Danelle
If you have done it correctly, you can also select files. Look through the documentation, maybe you find your problem thereHerbst
V
0

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);
}
}
Vulcanize answered 20/7 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.