file_picker 1.1.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 96

pub package Awesome Flutter

file_picker

File picker plugin alows you to use a native file explorer to load absolute file path from different file types.

Installation

First, add file_picker as a dependency in your pubspec.yaml file.

file_picker: ^1.1.1

Android

Add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> to your app AndroidManifest.xml file.

iOS

Since we are using image_picker as a dependency from this plugin to load paths from gallery and camera, we need the following keys to your Info.plist file, located in <project root>/ios/Runner/Info.plist:

  • NSPhotoLibraryUsageDescription - describe why your app needs permission for the photo library. This is called Privacy - Photo Library Usage Description in the visual editor.
  • NSCameraUsageDescription - describe why your app needs access to the camera. This is called Privacy - Camera Usage Description in the visual editor.
  • NSMicrophoneUsageDescription - describe why your app needs access to the microphone, if you intend to record videos. This is called Privacy - Microphone Usage Description in the visual editor.
  • UIBackgroundModes with the fetch and remote-notifications keys - describe why your app needs to access background taks, such downloading files (from cloud services) when not cached to locate path. This is called Required background modes, with the keys App download content from network and App downloads content in response to push notifications respectively in the visual editor (since both methods aren't actually overriden, not adding this property/keys may only display a warning, but shouldn't prevent its correct usage).

Usage

There's only one method within this package FilePicker.getFilePath() this receives 2 optional parameters, the fileType and a fileExtension to be used along with FileType.CUSTOM. So, 2 basically usages may be:

await FilePicker.getFilePath(type: FileType.ANY); // will display all file types
await FilePicker.getFilePath(type: FileType.CUSTOM, fileExtension: 'svg'); // will filter and display only files with SVG extension.

Note: When using FileType.CUSTOM, unsupported extensions will throw a MissingPluginException that is handled by the plugin.

Currently supported features

  • [X] Load paths from cloud files (GDrive, Dropbox, iCloud)
  • [X] Load path from gallery
  • [X] Load path from camera
  • [X] Load path from video
  • [X] Load path from any type of file (without filtering)
  • [X] Load path from a custom format by providing a file extension (pdf, svg, zip, etc.)

Demo App

Demo

Example

import 'package:file_picker/file_picker.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _filePath;

  void getFilePath() async {
   try {
      String filePath = await FilePicker.getFilePath(type: FileType.ANY);
      if (filePath == '') {
        return;
      }
      print("File path: " + filePath);
      setState((){this._filePath = filePath;});
    } on PlatformException catch (e) {
      print("Error while picking the file: " + e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('File Picker Example'),
      ),
      body: new Center(
        child: _filePath == null
            ? new Text('No file selected.')
            : new Text('Path' + _filePath),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: getFilePath,
        tooltip: 'Select file',
        child: new Icon(Icons.sd_storage),
      ),
    );
  }
}

Getting Started

For help getting started with Flutter, view our online documentation.

For help on editing plugin code, view the documentation.

1.1.1

  • Updates README file.

1.1.0

Breaking changes

  • FileType.PDF was removed since now it can be used along with custom file types by using the FileType.CUSTOM and providing the file extension (e.g. PDF, SVG, ZIP, etc.).
  • FileType.CAPTURE is now FileType.CAMERA

New features

  • Now it is possible to provide a custom file extension to filter file picking options by using FileType.CUSTOM

Bug fixes and updates

  • Fixes file names from cloud on Android. Previously it would always display Document
  • Fixes an issue on iOS where an exception was being thrown after canceling and re-opening the picker.
  • Fixes an issue where collision could happen with request codes on Android.
  • Adds public documentation to file_picker
  • Example app updated.
  • Updates .gitignore

1.0.3

  • Fixes build.gradle.

1.0.2

  • Minor update of README file.

1.0.1

  • Adds comments for public API

1.0.0

  • Version 1.0 release.
  • Adds support for ANY and VIDEO files.
  • Fixes an issue where permissions were recursively asked on Android.
  • Fixes an issue where some paths from document files couldn't be loaded with Android 8.0.
  • Updates README file to match changes.
  • General refactor & cleanup.

0.1.6

  • Replaces commons dependency with FilePath class on Android, to handle path resolution on different SDK.

0.1.5

  • Minor correction on README file.

0.1.4

  • Changed Meta minimum version due to versioning conflict with flutter_localization.

0.1.3

  • Updated readme.

0.1.2

  • Changed license from Apache 2.0 to MIT.
  • Adds demo screenshot.

0.1.1

  • Adds license information (Apache 2.0).
  • Adds CHANGELOG details.

0.1.0

  • Initial realise.
  • Supports picking paths from files on local storage, cloud.
  • Supports picking paths from both gallery & camera due to image_picker dependency.

example/lib/main.dart

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';
import 'package:file_picker/file_picker.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _fileName = '...';
  String _path = '...';
  String _extension;
  bool _hasValidMime = false;
  FileType _pickingType;
  TextEditingController _controller = new TextEditingController();

  @override
  void initState() {
    super.initState();
    _controller.addListener(() => _extension = _controller.text);
  }

  void _openFileExplorer() async {
    if (_pickingType != FileType.CUSTOM || _hasValidMime) {
      try {
        _path = await FilePicker.getFilePath(type: _pickingType, fileExtension: _extension);
      } on PlatformException catch (e) {
        print("Unsupported operation" + e.toString());
      }

      if (!mounted) return;

      setState(() {
        _fileName = _path != null ? _path.split('/').last : '...';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Plugin example app'),
        ),
        body: SingleChildScrollView(
          child: new Center(
              child: new Padding(
            padding: const EdgeInsets.only(top: 50.0, left: 10.0, right: 10.0),
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Padding(
                  padding: const EdgeInsets.only(top: 20.0),
                  child: new DropdownButton(
                      hint: new Text('LOAD PATH FROM'),
                      value: _pickingType,
                      items: <DropdownMenuItem>[
                        new DropdownMenuItem(
                          child: new Text('FROM CAMERA'),
                          value: FileType.CAMERA,
                        ),
                        new DropdownMenuItem(
                          child: new Text('FROM GALLERY'),
                          value: FileType.IMAGE,
                        ),
                        new DropdownMenuItem(
                          child: new Text('FROM VIDEO'),
                          value: FileType.VIDEO,
                        ),
                        new DropdownMenuItem(
                          child: new Text('FROM ANY'),
                          value: FileType.ANY,
                        ),
                        new DropdownMenuItem(
                          child: new Text('CUSTOM FORMAT'),
                          value: FileType.CUSTOM,
                        ),
                      ],
                      onChanged: (value) => setState(() => _pickingType = value)),
                ),
                new ConstrainedBox(
                  constraints: new BoxConstraints(maxWidth: 150.0),
                  child: _pickingType == FileType.CUSTOM
                      ? new TextFormField(
                          maxLength: 20,
                          autovalidate: true,
                          controller: _controller,
                          decoration: InputDecoration(labelText: 'File type'),
                          keyboardType: TextInputType.text,
                          textCapitalization: TextCapitalization.none,
                          validator: (value) {
                            RegExp reg = new RegExp(r'[^a-zA-Z0-9]');
                            if (reg.hasMatch(value)) {
                              _hasValidMime = false;
                              return 'Invalid format';
                            }
                            _hasValidMime = true;
                          },
                        )
                      : new Container(),
                ),
                new Padding(
                  padding: const EdgeInsets.only(top: 50.0, bottom: 20.0),
                  child: new RaisedButton(
                    onPressed: () => _openFileExplorer(),
                    child: new Text("Open file picker"),
                  ),
                ),
                new Text(
                  'URI PATH ',
                  textAlign: TextAlign.center,
                  style: new TextStyle(fontWeight: FontWeight.bold),
                ),
                new Text(
                  _path ?? '...',
                  textAlign: TextAlign.center,
                  softWrap: true,
                  textScaleFactor: 0.85,
                ),
                new Padding(
                  padding: const EdgeInsets.only(top: 10.0),
                  child: new Text(
                    'FILE NAME ',
                    textAlign: TextAlign.center,
                    style: new TextStyle(fontWeight: FontWeight.bold),
                  ),
                ),
                new Text(
                  _fileName,
                  textAlign: TextAlign.center,
                ),
              ],
            ),
          )),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  file_picker: ^1.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:file_picker/file_picker.dart';
  
Version Uploaded Documentation Archive
1.1.1 Dec 27, 2018 Go to the documentation of file_picker 1.1.1 Download file_picker 1.1.1 archive
1.1.0 Dec 27, 2018 Go to the documentation of file_picker 1.1.0 Download file_picker 1.1.0 archive
1.0.3 Dec 7, 2018 Go to the documentation of file_picker 1.0.3 Download file_picker 1.0.3 archive
1.0.2 Dec 6, 2018 Go to the documentation of file_picker 1.0.2 Download file_picker 1.0.2 archive
1.0.1 Dec 6, 2018 Go to the documentation of file_picker 1.0.1 Download file_picker 1.0.1 archive
1.0.0 Dec 6, 2018 Go to the documentation of file_picker 1.0.0 Download file_picker 1.0.0 archive
0.2.0 Dec 6, 2018 Go to the documentation of file_picker 0.2.0 Download file_picker 0.2.0 archive
0.1.6 Sep 12, 2018 Go to the documentation of file_picker 0.1.6 Download file_picker 0.1.6 archive
0.1.5 Sep 11, 2018 Go to the documentation of file_picker 0.1.5 Download file_picker 0.1.5 archive
0.1.4 Sep 11, 2018 Go to the documentation of file_picker 0.1.4 Download file_picker 0.1.4 archive

All 14 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
93
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
96
Learn more about scoring.

We analyzed this package on Jan 15, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.10
  • Flutter: 1.1.7

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/file_picker.dart.

Run flutter format to format lib/file_picker.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
flutter 0.0.0
image_picker ^0.4.10 0.4.12
meta ^1.1.5 1.1.6 1.1.7
Transitive dependencies
collection 1.14.11
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8