flutter_file_picker

repository·master·Indexed 23 days ago

https://github.com/miguelpruivo/flutter_file_picker

A Flutter package providing access to native file explorers for picking single or multiple files, selecting directories, and saving files. It supports mobile, web, and desktop platforms (Android, iOS, macOS, Linux, Windows) with features such as extension filtering, cloud file support, and platform-specific configurations via AndroidOptions and LinuxOptions.

Tokens
3.9K
Snippets
11
Records
40
Agent score
80%

What's inside flutter_file_picker

  1. Manage memory when picking large files

    master

    When picking multiple or large files on mobile/desktop, avoid loading all bytes into memory using withData: true, as this can cause out-of-memory errors. Instead, use withReadStream: true and keep withData: false to handle files efficiently.

    FilePickerResult? result = await FilePicker.pickFiles(
      allowMultiple: true,
      withData: false,
      withReadStream: true,
    );
  2. Customize iOS launch screen assets

    master

    To change the launch screen image for the iOS version of the application, you can either replace the image files directly in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode to manage the assets.

    To use Xcode:

    1. Open the iOS project using open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, select Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog.
    open ios/Runner.xcworkspace
  3. Regenerate DBus bindings for Linux development

    master

    If you are developing the Linux implementation, the DBus bindings in lib/src/xdp_filechooser.dart and lib/src/xdp_request.dart are generated using dart-dbus from the XDG Desktop Portal DBus interface specifications.

    To regenerate these bindings, follow these steps:

    1. Install dart-dbus globally via Pub.
    2. Regenerate the bindings against a running DBus daemon or XML interface definition using the org.freedesktop.portal.FileChooser and org.freedesktop.portal.Request interfaces.
    # Install dart-dbus
    dart pub global activate dbus
    
    # Regenerate bindings
    dart-dbus generate-remote-object org.freedesktop.portal.FileChooser
    dart-dbus generate-remote-object org.freedesktop.portal.Request
  4. Implement a custom platform for file_picker

    master

    To create a new platform plugin for the file_picker ecosystem, you must extend the FilePickerPlatform class. After implementing the required methods, you must register your implementation by assigning it to FilePickerPlatform.instance within a registration method (conventionally named registerWith).

    class FilePickerCustomPlatform extends FilePickerPlatform {
      static void registerWith() {
        FilePickerPlatform.instance = FilePickerCustomPlatform();
      }
    
      // Implement methods...
    }
  5. Configure Web file loading behavior with WebOptions

    master

    When picking files on the Web, you can control how file data is loaded into memory using WebOptions. This is crucial for managing memory usage with large files.

    • withData (default: true): If true, the PlatformFile will contain the full file content in its bytes property. This loads the entire file into memory.
    • withReadStream: If true, the PlatformFile will provide a readStream to read the file in chunks (1 MB chunks), which is more memory-efficient for large files than loading all bytes at once.
  6. Configure Linux-specific options for FilePicker

    master

    When using the file picker on Linux, you can pass LinuxOptions to customize the behavior of the XDG Desktop Portal. One key option is lockParentWindow, which determines if the picker dialog should be modal (locking the parent window).

    These options are passed via the linuxOptions parameter in methods like pickFiles, getDirectoryPath, and saveFile.

  7. Access file details and XFiles

    master

    A FilePickerResult provides access to PlatformFile objects. You can retrieve metadata directly from the PlatformFile or convert them to XFile (from the cross_file package) for easier integration with other libraries.

    FilePickerResult? result = await FilePicker.pickFiles();
    
    if (result != null) {
      // Accessing PlatformFile details
      PlatformFile file = result.files.first;
      print(file.name);
      print(file.bytes);
      print(file.size);
      print(file.extension);
      print(file.path);
    
      // Retrieving as XFiles
      List<XFile> xFiles = result.xFiles;
      XFile xFile = result.files.first.xFile;
    } else {
      // User canceled the picker
    }