Path Provider in Flutter

Learn via video courses
Topics Covered

The Path Provider Flutter package is a crucial asset for Flutter developers, streamlining file system interactions within applications. It offers convenient access to key locations like temporary and document directories, facilitating seamless file read and write operations. This is especially vital for apps involving photo storage or document creation, where determining the correct storage location is essential. Furthermore, the package grants easy entry to the external storage directory, a valuable feature for handling substantial files or implementing data backup functionalities. Effectively, the Flutter path provider serves as a cornerstone in Flutter applications, simplifying file operations for various formats such as text files, PDFs, and images. In essence, mastering this tool empowers developers to handle files efficiently, making it an indispensable component in the Flutter development toolkit.

What is PathProvider in Flutter?

In mobile application development, it is often necessary to access various directories. PathProvider is a package provided by Flutter which makes it very convenient for us to do so. As it is capable of handling platform-specific differences, we can write a single code and it works on multiple platforms. It offers a unified API that enables developers to access directories and files easily. The platform awareness, extensive functionalities, ease of integration, and the plethora of file and directory management capabilities offered by the PathProvider package make it an indispensable tool for Flutter developers.

Installation and Setup

You can follow the steps given below to start using the PathProvider package:

1. Add the path_provider package to the pubspec.yaml file:

You can also do this by doing one of the following steps:

Press the Ctrl+shift+p and then click on add dependency.

Or

Simply run the following command on your terminal:

2. Import the PathProvider package

In the file where you want to use PathProvider you need to first import the package as follows:

3. Access Directory Paths

Now, after adding the package to our pubspec.yaml and importing it to our dart file, we can use the PathProvider package to access directory paths. The basic use cases for this are shown in the next section.

Basic Usage of Path Provider in Flutter

We will now utilize the APIs provided by PathProvider to access directory paths. We can use the getApplicationDocumentsDirectory(),getTemporaryDirectory() and getExternalStorageDirectory() for the same purpose. We can use them as shown below:

1. getApplicationDocumentsDirectory()

This method is used to access the application's document directory. It returns the path to the documents directory.

2. getTemporaryDirectory()

We use the getTemporaryDirectory() method to get the path to the temporary directory. It returns an object of the form Directory that represents the path to the temporary directory

3. getExternalStorageDirectory()

This method is used for retrieving the path to the external storage directory. We have shown it's working in the example given below.

Working with Files and Directories

The PathProvider package in Flutter offers multiple methods for working with files and directories. The PathProvider Flutter package provides us with various methods which make it very convenient for us as developers to work with files and directories.

In this section, we'll be looking at these operations one by one.

1. Creating the Directories

We use the Directory class to create a directory within a specific path. A new directory is created by using the getApplicationDocumentsDirectory() method. We store this directory in an object appDocDir of the File type Directory. Now we store the path of the directory which we want to create in a String named DirPath. Finally, by calling the Directory(DirPath) the new drectory is created.

2. Reading the Files

We use the readAsString method to read the contents of a file. The f object of the type File contains the path of the file which we want to read. The String of the name fileContent uses the readAsString() method to get the content of the f.

3. Writing to the files

The methodwriteAsString works similarly to as shown in the above example.

4. Deleting the Files and Directories

We can use the delete() method to delete the files and directories and remove them from the system. For deleting the file we pass the path into an object named f of the type File and call the delete function. We similarly delete the directories. Please note that in the delete method, we set the recursive parameter as true. This helps to perform the deletion recursively.

Example App

Github Repository Link: scaler_path_provider

example app

main.dart file:

Github Link: main.dart

This is the main.dart file which takes you to the Demo Page. All the functionalities of the app will take place from the DEmo page itself.

demo.dart file:

Github Link: demo.dart

This is the Demo page. It contains five elevated buttons. These buttons have some relevant text written on them and clicking on these buttons takes you to the respective pages. All the demonstration of the functionalities will be done on those respective pages itself. These buttons represent BasicUsage, Creating, Reading, Writing, Deleting respectively.

basic_usage.dart file:

Github Link: basic_usage.dart

The BasicUsage widget contains three elevated buttons. Each button performs one of the three operations which are: getApplicationDocumentsDirectory(), getTemporaryDirectory(), and getExternalStorageDirectory() respectively. When we press a button then the respective function is performed and then an AlertDialog is displayed corresponding to the function. The role of this widget is to demonstrate the basic functions supported by the PathProvider in Flutter.

creating.dart file:

Github Link: creating.dart

As the name suggests, the role of this widget is to demonstrate the creation of a new directory using the PathProvider package in Flutter. When we press the ElevatedButton, it retrieves the applications directory using the getApplicationDocumentsDirectory() method and then creates a new directory by concatenating the path with /new_directory. After the directory is created using the create() method we show an AlertDialog with the desired message.

reading.dart file:

Github Link: reading.dart

This widget demonstrates how we can use the PathProvider Flutter package to read the files from a directory. Like all the previous methods, this method is also implemented with the help of an Elevated button. When we press the button, it retrieves the applications directory using the getApplicationDocumentsDirectory() method. We then create a File object with the desired file path, in this case, '$documentsPath/my_file.txt'. We use the readAsString method to read the contents and finally, we show an AlertDialog with the desired message.

writing.dart file:

Github Link: writing.dart

As the name suggests, the Writing widget demonstrates the way to write to a directory by using the PathProvider flutter package. In a similar fashion, as we have been doing for the other widgets, for Implementing the Writing widget, we have an ElevatedButton. When the button is pressed by the user, we retrieve the application's documents directory using getApplicationDocumentsDirectory(). We then create a File object with the desired file path, in this case, '$documentsPath/my_file.txt'. We define the content to be written to the file as a string, in this case, 'Hello, Scaler Topics this side!'. We use the writeAsString method of the File class to write the content to the file. finally, we display the desired output in the AlertDialog.

deleting.dart file:

Github Link: deleting.dart

This is the final widget to be redirected from the Demo widget. This widget demonstrates the deletion of a directory through the usage of the PathProvider Flutter package. To perform the deletion we follow some steps sequentially as mentioned.

Firstly, the application's documents directory is retrieved by using thegetApplicationDocumentsDirectory() method. Then a file object is created with the path to the file to be deleted. Now the delete() method is called on it to delete the file. Finally, like all the above cases, we show the desired output using the AlertDialog.

Conclusion

  1. The PathProvider package makes it very simple for the developers to manage files and directories in Flutter applications.
  2. It can adapt to different platforms such as Android and iOS.
  3. The APIs offered by the package enable us to perform various operations seamlessly.
  4. We use the PathProvider package to effortlessly retrieve path directories like the documents directory, temporary directory, and external storage.
  5. PathProvider is thus, a very reliable tool and helps us to build file handling mechanisms for our flutter applications.