Downloading Files From Firebase Storage on Flutter

STEPHEN HANDIAR CHRISTIAN
3 min readJun 6, 2021

--

I will tell you about how I implemented the download function in the Pantau Peradilanmu project

Photo by Sigmund on Unsplash

There are several ways to download files on Flutter. Despite how many references there are, very few references to downloading files from firebase storage. This problem makes me want to tell you how I implemented it to download files from firebase storage.

First, we need some packages that are used as dependencies to be able to download files. We need firebase_core, a Flutter plugin to use the Firebase Core API, it enables connecting to multiple Firebase apps. Then, we also need firebase_storage, a Flutter plugin to use the Firebase Cloud Storage API. We need open_file too, a plugin that can call native APP to open files with string result in Flutter. Finally, path_provider, a Flutter plugin for finding commonly used locations on the filesystem. We can add these packages to our pubsec.yaml.

Here’s an example of code dependencies on our pubspec.yaml:

dependencies:
firebase_core: ^1.2.1
firebase_storage: ^8.1.1
open_file: ^3.2.1
path_provider: ^2.0.2

You can also check the website for each package to get the latest version of the existing packages. You can check from the link in the reference below☺.

Then, after adding and running flutter pub get, we added the following snippet to our code page to create the carousel:

In the code snippet, I created a method called downloadFile() which runs asynchronously. In the downloadFile() method, I make a ref variable which is a reference to the file in our firebase storage. You can replace ‘’panduan/Pedoman_pemantauan.pdf’ with the location where your files are stored in your firebase storage. On lines 6–7, the code prepares the location for the file to be downloaded to be stored. In line 8, the code prepares a temporary file for the file to be downloaded where you can replace ‘Pedoman_Pemantauan.pdf’ with the file name you want to save when the file is downloaded. Then, lines 10–11 are the code that saves/writes to the file, and line 12, OpenFile.open() tries to open the file that was downloaded earlier. Finally, on lines 13–18, a snack bar will appear if there is a failure/error while downloading, and for lines 23–32, it will display a snack bar that conveys a message that the file has been downloaded successfully.

To use the method is quite easy. We only need to put on the button/widget that will download the file when clicked. Here is an example of using the button:

In that code snippet, I created an ElevatedButton. In the onPressed(line 9) parameter, I call the downloadFile() function asynchronously.

Here are the results of the download functionality that we created in the Pantau Peradilanmu Application.

Well, that is our way to downloading files from firebase storage for the Pantau Peradilanmu application.

End Word

I created this article to provide additional references for the implementation of download functionality from firebase storage on Dart and the Flutter framework. What I wrote above is an implementation that we made for the Pantau Peradilanmu project. Hopefully, this article is useful and makes your knowledge increase.

Reference

--

--