Skip to content
Miguel Ruivo edited this page Aug 10, 2021 · 49 revisions

Filters

All the paths can be filtered using one of the following enumerations:

Filter Description
FileType.any Will let you pick all available files. On iOS it opens the Files app.
FileType.custom Will let you pick a path for the extension matching the allowedExtensions provided. Opens Files app on iOS.
FileType.image Will let you pick an image file. Opens gallery (Photos app) on iOS.
FileType.video Will let you pick a video file. Opens gallery (Photos app) on iOS.
FileType.media Will let you pick either video or images. Opens gallery (Photos app) on iOS.
FileType.audio Will let you pick an audio file. Opens music on iOS. Note that DRM protected files won't provide a path, null will be returned instead.

Parameters

There are a few common parameters that all picking methods support, those are listed below:

Parameter Type Description Default
type FileType Defines the type of the filtered files. FileType.any
allowedExtensions List<String>? Accepts a list of allowed extensions to be filtered. Eg. [.pdf, .jpg] -
allowCompression bool Defines whether image and/or video files should be compressed automatically by OS when picked. On Android has no effect as it always returns the original or integral file copy. true
withData bool Sets if the file should be immediately loaded into memory and available as Uint8List on its PlatformFile instance. true on Web, false everywhere else
withReadStream bool Allows the file to be read into a Stream<List<int>> instead of immediately loading it into memory, to prevent high usage, specially with bigger files. If you want an example on how to use it, check it here. false
onFileLoading Function(FilePickerStatus)? When provided, will receive the processing status of picked files. This is particularly useful if you want to display a loading dialog or so when files are being downloaded/cached -
dialogTitle String? The title to be set on desktop platforms modal dialog. Hasn't any effect on Web or Mobile. -

Methods

pickFiles()

This is the main method to pick files and provides all the properties mentioned before. Will return a FilePickerResult — containing the List<PlatformFile>> — or null if picker is aborted.

NOTE: You must use FileType.custom when providing allowedExtensions, else it will throw an exception.

NOTE 2: On web the path will always be null as web always use fake paths, you should use the bytes instead to retrieve the picked file data.

Usage example

// Will let you pick one file path, from all extensions
FilePickerResult result = await FilePicker.platform.pickFiles(type: FileType.any);

if(result != null) {
 File file = File(result.files.first.path);
}

// Will filter and only let you pick files with svg and pdf extension
FilePickerResult result = await FilePicker.platform.pickFiles(type: FileType.custom, allowedExtensions: ['svg', 'pdf']);

if(result != null) {
 File file = File(result.files.first.path);
}

getDirectoryPath()

Will let you select and pick a directory path. Optionally, you can set a dialogTitle on desktop platforms.

Platform Info
iOS Requires iOS 11 or above
Android Requires SDK 21 or above
Desktop Supported
Web Not supported

clearTemporaryFiles()

An utility method that will explicitly prune cached files from the picker. This is not required as the system will take care on its own, however, sometimes you may want to remove them, specially if your app handles a lot of files.

Platform Info
iOS All picked files are cached, so this will immediately remove them.
Android Since 2.0.0, all picked files are cached, so this will immediately remove them.
Desktop & Web Not implemented as it won't have any effect. Paths are always referencing the original files.
Clone this wiki locally