Download files with Cloud Storage on Flutter
Notice
This page is archived and might not reflect the latest version of the FlutterFire plugins. You can find the latest information on firebase.google.com:
https://firebase.google.com/docs/storage/flutter/download-files
Cloud Storage for Firebase allows you to quickly and easily download files from a Cloud Storage bucket provided and managed by Firebase.
note
By default, a Cloud Storage bucket requires Firebase Authentication to perform any action on the bucket's data or files. You can change your Firebase Security Rules for Cloud Storage to allow unauthenticated access. Since Firebase and your project's default App Engine app share this bucket, configuring public access may make newly uploaded App Engine files publicly accessible, as well. Be sure to restrict access to your Cloud Storage bucket again when you set up Authentication.
#
Create a ReferenceTo download a file, first create a Cloud Storage reference to the file you want to download.
You can create a reference by appending child paths to the root of your
Cloud Storage bucket, or you can create a reference from an existing
gs://
or https://
URL referencing an object in Cloud Storage.
#
Download FilesOnce you have a reference, you can download files from Cloud Storage
by calling the getData()
or getStream()
. If you prefer to download the file
with another library, you can get a download URL with getDownloadUrl()
.
#
Download in memoryDownload the file to a UInt8List
with the getData()
method. This is the
easiest way to download a file, but it must load the entire contents of
your file into memory. If you request a file larger than your app's available
memory, your app will crash. To protect against memory issues, getData()
takes a maximum amount of bytes to download. Set the maximum size to something
you know your app can handle, or use another download method.
#
Download to a local fileThe writeToFile()
method downloads a file directly to a local device. Use this if
your users want to have access to the file while offline or to share the file in a
different app. writeToFile()
returns a DownloadTask
which you can use to manage
your download and monitor the status of the download.
#
Download Data via URLIf you already have download infrastructure based around URLs, or just want
a URL to share, you can get the download URL for a file by calling the
getDownloadURL()
method on a Cloud Storage reference.
#
Handle ErrorsThere are a number of reasons why errors may occur on download, including the file not existing, or the user not having permission to access the desired file. More information on errors can be found in the Handle Errors section of the docs.
#
Full ExampleA full example of a download with error handling is shown below:
You can also get and update metadata for files that are stored in Cloud Storage.