Download a file on firebase

Flutter tips Published on

Hey Flutter devs!

Want to grab a file from Firebase Storage and show your users how the download is going? This quick guide shows you a neat way to do it.

Getting Started

First things first, you need to make sure your app has permission to save files on the device. We use the permission_handler plugin for this. Always ask the user nicely!

if (await Permission.storage.request().isDenied) {
  throw "Vous devez accepter la permission d'écrire";
}

This code checks if storage permission is denied and throws an error if it is, reminding the user to grant it.

Finding the File on Firebase

Next, we get a reference to the file you want to download using its path in Firebase Storage.

var fileRef = storage.ref().child(path);
var fileName = fileRef.name;

This gets the file reference and its name, which we'll need later.

Where to Save the File?

We need a spot on the device to save the downloaded file. The code picks a directory based on the platform.

Android Specifics

On Android, it defaults to the common Download folder.

Directory directory;
if (Platform.isAndroid) {
  directory = Directory("/storage/emulated/0/Download");
} else {
  directory = (await getExternalStorageDirectory())!;
}

For other platforms, it uses the external storage directory provided by a helper function.

Preparing the Destination

We build the full path for the final file and make sure the directory exists.

final File destinationFile = File('${directory.path}/$fileName');
Handling Existing Files

What if a file with the same name is already there? This code snippet deletes the old one before saving the new one. You might want to handle this differently depending on your app's needs.

if (destinationFile.existsSync()) {
  destinationFile.deleteSync();
}
destinationFile.createSync();

Starting the Download and Tracking Progress

Now, the fun part! We start the download using writeToFile on the Firebase storage reference.

var task = fileRef.writeToFile(destinationFile);
yield* task.asStream();

The yield* keyword combined with task.asStream() is super useful here. It turns the download task into a Stream. This means you can listen to this stream and get updates on the download progress as it happens.

Calculating Progress

To show how much of the file has been downloaded, you can use the information from the stream updates:

var progress = task.bytesTransferred / task.totalBytes * 100;

This gives you the percentage of the download completed.

And that's it! You can now download files from Firebase Storage and easily display the download progress in your Flutter app.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

Do I need special permissions to download files?

Yes, you need storage permission on the device to save the downloaded file. The example uses the `permission_handler` plugin to request this.

How do I get the download progress?

The `writeToFile` method returns a task that can be converted into a stream using `task.asStream()`. You can listen to this stream to get updates on bytes transferred and total bytes, then calculate the percentage.

Where does the downloaded file get saved?

On Android, the example saves it to the default 'Download' folder. On other platforms, it uses the external storage directory provided by `getExternalStorageDirectory()`.

What happens if the file I'm downloading already exists on the device?

The provided code checks if the file exists at the destination path and deletes the old file before saving the new one.

What Flutter packages are needed for this?

You'll need `firebase_storage` for the download itself and `permission_handler` to manage storage permissions.

Read more
You may also be interested in
Go Beyond Material: Add Custom Colors in your Flutter Theme  blog card image
Go Beyond Material: Add Custom Colors in your Flutter Theme
Published on 2025-05-12T08:49:42.146Z
Delegate widget design  blog card image
Delegate widget design
Published on 2025-05-12T09:03:59.853Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved