Hrishikesh Pathak

Use file picker in flutter

flutter
Use file picker in flutter

Hi developers, in this article, we are discussing how to use a file picker in Flutter. I am trying to cover all aspects and use cases you may encounter during implementation of a file picker in flutter. This article, or you can say guide, is applicable for android, iOS, and web platforms.

We are going to use file_picker package from pub.dev.

Before starting, let's discuss some handy features of this file picker.

Features of file picker

  • It uses operating system default native picker : This file picker invoke native file picker and get information of user selected file. Thus, we can see OS specific UI in this file picker package.
  • Filter different types of files : If you want only a specific type of file (e.g. PDF, JPG, SVG etc), you can set this file extensions as an argument and file picker will show user the requested file types, nothing else.
  • Pick files from cloud files : This extension allow you to pick files not only from your device, but also from cloud storage like Google Drive, Dropbox, iCloud etc.
  • Single or multiple : You can allow user to select single or multiple file at once. This is up to you, as a developer you have the freedom to decide what you want in your application.
  • Read file : This file picker extension can read the selected file and load it in the memory as a Uint8List. This will be very handy if you want to upload this file somewhere, or you want to manipulate it as your own way.

So the possibility is endless. It is up to you as a developer that what you choose and what you leave. Before make that decision, let's see how to implement all these feature in code. Let's open our code editor and make a new flutter project.

A new flutter project

Probably all of you know how to create a new project in flutter, right? Those of you who don't know, just open your terminal and run.

1flutter create <your project name>
2

After you create the project, go to the project folder and open in your code editor. In my case, I am using VS Code.

So after everything has set up, let's make a file picker using flutter.

Implementing a file picker in Flutter

So before writing any code, we have to install file_picker package from pub.dev.

Head on to your pubspec.yaml file and add the following line in your file.

1// pubspec.yaml
2
3name: flutter_file_picker
4description: A new Flutter project.
5version: 1.0.0+1
6
7environment:
8 sdk: ">=2.12.0 <3.0.0"
9
10dependencies:
11 flutter:
12 sdk: flutter
13 cupertino_icons: ^1.0.2
14 // Add this line in your file
15 file_picker: ^3.0.3
16
17dev_dependencies:
18 flutter_test:
19 sdk: flutter
20
21flutter:
22 uses-material-design: true
23

Now finally we are ready to write our file picker. Are you excited ? If you are enjoying this article, then you can press the heart button to give me heart 😊. Easy eight ? OK, let's focus on code.

So go to your lib/main.dart file and delete all the code right there, as we start from scratch.

As we will use material widgets and file picker package in this tutorial, at first import these two packages at the top of our main.dart file.

1// main.dart
2
3import 'package:flutter/material.dart';
4import 'package:file_picker/file_picker.dart';
5

Then let's write our main function. This is the starting point of our application. I assume you know all these already, so I will fast-forward to file picker implementation. I have written a bare-bones UI, so that we can focus on the implementation.

1// main.dart
2
3import 'package:flutter/material.dart';
4import 'package:file_picker/file_picker.dart';
5
6void main() {
7 runApp(
8 MaterialApp(
9 home: FilePickerApp(),
10 ),
11 );
12}
13
14class FilePickerApp extends StatelessWidget {
15
16 Widget build(BuildContext context) {
17 return Scaffold(
18 appBar: AppBar(
19 title: Text("This is for only tut perpose"),
20 ),
21 body: Center(
22 child: ElevatedButton(
23 onPressed: () {
24 // Add file picker functionality here
25 },
26 child: Text("File Picker"),
27 ),
28 ),
29 );
30 }
31}
32

Here I made a scaffold and at the centre, I have made a ElevatedButton.

When user press the button, We lunch the file picker.

For that, inside the onPressed method, we await for the user to pick a file. For that, we run FilePicker.platform.pickFiles() function.

This function can get us two possible outcomes. If user successfully pick the file, this give us a FilePickerResult or if user don't choose any file, it returns a null.

So to handle that two condition, we apply a null check that if the result is not null, then give us the name of the file user have picked.

In code, the file picker will look like this :

1// main.dart
2
3onPressed: () async {
4 FilePickerResult? result = await FilePicker.platform.pickFiles();
5 if (result == null) {
6 print("No file selected");
7 } else {
8 print(result.files.single.name);
9 }
10}
11

You can extend this file picker and customize this as your need. As I have previously mentioned, you can filter your file type, choose between single or multiple file and even pick a file from a cloud drive.

The possibility is endless. So try to make your own customize file picker and share with me. You can find me on Twitter as @hrishikshpathak.

See you next time.