file_picker_cross
the only flutter plugin to select, open, choose, pick and create documents, images videos or other files on android, ios, the desktop and the web for reading, writing, use as string, byte list or http uploads.
getting started
file_picker_cross
allows you to select, edit and save files from your device and is compatible with android, ios, desktops (using both go-flutter or fde) and the web.
note: we recently had api changes. please update your code accordingly.
// show a dialog to open a file
filepickercross myfile = await filepickercross.importfromstorage(
type: filetypecross.any, // available: `any`, `audio`, `image`, `video`, `custom`. note: not available using fde
fileextension: 'txt, md' // only if filetypecross.custom . may be any file extension like `dot`, `ppt,pptx,odp`
);
// save our file to the fictional directory. it is not necessary that it already exists.
myfile.savetopath('/my/awesome/folder/' + myfile.filename);
// save our file to the internal storage or share to other apps
myfile.exporttostorage();
// for sharing to other apps you can also specify optional `text` and `subject`
myfile.exporttostorage(
subject: "awesome file",
text: "here is the file you've been waiting for",
);
// on ipad you may also need to specify the `sharepositionorigin` for native share ui
globalkey widgetkey = globalkey();
...
renderbox renderbox = widgetkey.currentcontext.findrenderobject();
offset position = renderbox.localtoglobal(offset.zero);
filepickercross.exporttostorage(
position.dx, position.dy, renderbox.size.width, renderbox.size.height);
list<filepickercross> mymultiplefiles = await filepickercross.importmultiplefromstorage();
print(mymultiplefiles);
// list all previously opened files
list<string> paths = await filepickercross.listinternalfiles();
print(paths);
// open an existing file
filepickercross anotherfile = filepickercross.frominternalpath(paths[0]);
// delete our perfect file
filepickercross.delete(paths[0]);
// get the file storage size
print(await filepickercross.quota());
// you can access the following properties on a filepickercross instance:
myfile.tostring();
myfile.touint8list();
myfile.tobase64();
myfile.tomultipartfile(filename: 'myfile.txt');
myfile.length;
myfile.path;
myfile.filename;
myfile.fileextension;
myfile.directory;
to get details about the certain properties and methods, check out the api documentation.
exception handling
different platforms will throw different exceptions whether it is due to user action or platform restrictions. for instance, you may want to know if a user had denied access to storage and act upon it. there is a method to help out with that.
await filepickercross.importfromstorage().then(() {
// ...
}).onerror((error, _) {
string _exceptiondata = error.reason();
print('----------------------');
print('reason: ${_exceptiondata}');
if (_exceptiondata == 'read_external_storage_denied') {
print('permission was denied');
} else if (_exceptiondata == 'selection_canceled') {
print('user canceled operation');
}
print('----------------------');
});
when the fileselectioncancelederror
exception is thrown, you can access the reason()
method to collect underlying exception information. it has a return type of string
.
behavior:
- on user cancelation,
selection_canceled
is returned - when
platformexception
exception occurs, the raw error value is extracted and then returned (i.e.read_external_storage_denied
) - as a fallback, exceptions that were not ‘handled’ will be returned in full
the scope of this package
tl;dr: we provide a parallel, platform-independent implementation of a fake file system, in which you can create, open and save files for your app – even on the web. moreover, we provide apis to interact with the real file system as well to import and export files from and to your device.
it is very difficult to handle files in cross platform apps. while desktops have one files system used for all apps, mobile platforms have isolated file systems for each app. the web does not really have a working file system available on all browsers. hence, it is hard to implement storage and access to files on all platforms – and you do not have to because we already did this for you.
with file_picker_cross
, we provide a fake file system for use in your app. unlike other packages, we do not only provide a dialog for reading or saving files, but we provide a whole file system inside your app’s storage, in which you can use any operation like searching files, opening them and saving them. of cause, there are apis too for importing files from the shared storage (device storage, home folder, etc.) or exporting to these – even on the web.
where files are saved
there are two important methods to export/save files: exporttostorage
and savetopath
.
exporttostorage
shows a dialog and allows the user to select where to save the file.savetopath
is meant for automated saving in case files are automatically created by an application for further use only within the application. for the web, it means, files are stored in the localstorage, on windows, the path is%localappdata%your_app_name
and on all other platforms the files are stored in${getapplicationdocumentsdirectory()}/your_app_name/
.
a word on directories
why is isn’t it possible to pick directories? that’s what we are asked commonly. there is a simple reason: mobile and web device’s security mechanisms.
anyway, there are two workarounds available, depending on what you plan to do.
if you have plenty of files, you simply need to store somewhere, you may use our provided savetopath('/my/path')
api. this allows you to save any file to an app-internal path. see the api documentation for further details.
another use case is saving files to a user-defined directory. for single files, you may use the exporttostorage()
api (documentation). but if you want to once pick a directory and save continuously save and read files there, it will generally be impossible on most devices except of desktops. all other device types prevent this by their security mechanisms. on desktops (and unfortunately only on desktops), there is a workaround available:
// for the first file, you show an export as dialog
filepickercross myfile = ...
string pathforexports = await myfile.exporttostorage(); // <- will return the file's path on desktops
// you parse the file's directory and use it for later automated exports.
pathforexports = pathforexports.substring(0,pathforexports.lastindexof(r'/'));
print(pathforexports);
// now save the path for later use using shared preferences etc.
...
// next time, check whether you are overwriting an existing file or simply write the file
print(await file(pathforexports+'/mynextfile.csv').exists());
file mycsvfile = await file(pathforexports+'/mynextfile.csv').writeasstring('comma,separated,values'); // <- this only works on desktops. all other devices prevent this.
(source: issue #11)
moreover, we plan to support direct write access on certain shared storage locations of the device like documents, downloads etc. our plans on that as well as the api are not ready yet but you might expect this to be supported.
go-flutter and fde
flutter initially only supported android and ios. to add support for desktop platforms, some people started the go-flutter providing flutter applications on windows, linux and macos using the go language.
later, flutter itself announced desktop support (fde) but still, it’s not stable yet.
we try to support both as much as possible.
web
of cause, it requires flutter to be set up for web development.
all desktop platforms
of cause, it requires flutter to be set up for your platform.
please note, windows is not officially supported by google. linux and macos support is in alpha state. expect issues and sometimes incompatible versions requiring manual hand work.
flutter channel dev # or master
flutter upgrade
flutter config --enable-linux-desktop
flutter config --enable-macos-desktop
flutter config --enable-windows-desktop
more information
set up go-flutter or set up fde
mobile platforms
no setup required ��.
macos (using fde)
you will need to add an
entitlement
for either read-only access:
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
or read/write access:
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
depending on your use case.
linux (using fde)
this plugin requires the following libraries:
- gtk 3
- pkg-config
- xdg-user-dirs
installation example for debian-based systems:
sudo apt-get install libgtk-3-dev pkg-config xdg-user-dirs
note: you do no longer have to modify any files unlike in previous versions.
Comments are closed.