Skip to content

Latest commit

 

History

History
102 lines (81 loc) · 4.26 KB

README.md

File metadata and controls

102 lines (81 loc) · 4.26 KB

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

  • Load paths from cloud files (GDrive, Dropbox, iCloud)
  • Load path from gallery
  • Load path from camera
  • Load path from video
  • Load path from any type of file (without filtering)
  • 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.