Cloud Storage lets your app save and get files like photos or documents online. This means users can access their files from any device.
Cloud Storage for files in Flutter
final storageRef = FirebaseStorage.instance.ref(); // Upload a file await storageRef.child('folder/filename.jpg').putFile(file); // Download a file URL String url = await storageRef.child('folder/filename.jpg').getDownloadURL();
Use FirebaseStorage.instance.ref() to get the root reference.
Use child('path') to point to a file or folder.
photo.png inside the images folder.final storageRef = FirebaseStorage.instance.ref(); await storageRef.child('images/photo.png').putFile(file);
report.pdf inside the docs folder.String url = await storageRef.child('docs/report.pdf').getDownloadURL();
user_uploads where you can add files.final folderRef = storageRef.child('user_uploads');
This app lets the user pick an image from their gallery, uploads it to Firebase Cloud Storage, and then shows the download URL.
The image preview appears after selection, and the URL appears after upload.
import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_storage/firebase_storage.dart'; import 'package:image_picker/image_picker.dart'; import 'dart:io'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp()); } class MyApp extends StatefulWidget { @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { File? _image; String _downloadUrl = ''; Future<void> _pickAndUploadImage() async { final picker = ImagePicker(); final pickedFile = await picker.pickImage(source: ImageSource.gallery); if (pickedFile == null) return; setState(() { _image = File(pickedFile.path); _downloadUrl = ''; }); final storageRef = FirebaseStorage.instance.ref(); final fileRef = storageRef.child('uploads/${DateTime.now().millisecondsSinceEpoch}.png'); await fileRef.putFile(_image!); final url = await fileRef.getDownloadURL(); setState(() { _downloadUrl = url; }); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Cloud Storage Example')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ _image == null ? Text('No image selected.') : Image.file(_image!, width: 150, height: 150), SizedBox(height: 20), ElevatedButton( onPressed: _pickAndUploadImage, child: Text('Pick and Upload Image'), ), SizedBox(height: 20), _downloadUrl.isEmpty ? Container() : SelectableText('Download URL:\n$_downloadUrl', textAlign: TextAlign.center), ], ), ), ), ); } }
Make sure to initialize Firebase in your app before using Cloud Storage.
Use unique file names to avoid overwriting files.
Handle permissions to access gallery or camera on the device.
Cloud Storage lets apps save and get files online easily.
Use FirebaseStorage.instance.ref() to start working with files.
Upload files with putFile() and get URLs with getDownloadURL().
Practice
FirebaseStorage.instance.ref() in Flutter apps?Solution
Step 1: Understand FirebaseStorage.instance.ref()
This method creates a reference to a specific location in Firebase Cloud Storage where files can be managed.Step 2: Identify its role in file handling
It is used to point to folders or files for uploading or downloading, not for app initialization or user authentication.Final Answer:
To create a reference to a location in cloud storage where files can be uploaded or downloaded. -> Option BQuick Check:
ref() creates storage location reference [OK]
- Confusing ref() with Firebase app initialization
- Thinking ref() handles user authentication
- Assuming ref() accesses local files
imageFile to Firebase Storage in Flutter?Solution
Step 1: Check the correct method for uploading files
The method to upload a file isputFile()called on a reference fromFirebaseStorage.instance.ref().Step 2: Match the syntax
await FirebaseStorage.instance.ref('images/image.png').putFile(imageFile); correctly usesawait FirebaseStorage.instance.ref('path').putFile(file). Other options use incorrect method names or call patterns.Final Answer:
await FirebaseStorage.instance.ref('images/image.png').putFile(imageFile); -> Option AQuick Check:
Use ref().putFile() to upload [OK]
- Using incorrect method names like uploadFile or upload
- Calling putFile directly on FirebaseStorage without ref()
- Omitting await for asynchronous upload
final ref = FirebaseStorage.instance.ref('docs/report.pdf');
final url = await ref.getDownloadURL();
print(url);
What will be printed by this code?Solution
Step 1: Understand getDownloadURL()
This method returns a URL string that points to the file stored in Firebase Storage at the given reference.Step 2: Analyze the code output
The code prints the URL string, not local paths, errors, or file content.Final Answer:
A string URL that can be used to download the file 'report.pdf' from Firebase Storage. -> Option DQuick Check:
getDownloadURL() returns file URL [OK]
- Thinking it returns local file path
- Expecting file content instead of URL
- Assuming error if file not uploaded (only if missing)
final ref = FirebaseStorage.instance.ref('uploads/photo.jpg');
ref.putFile(photoFile);
What is the likely cause of the error?Solution
Step 1: Check asynchronous call usage
The methodputFile()returns a Future and should be awaited or handled properly.Step 2: Identify missing await
Not usingawaitor.then()can cause errors or unhandled Futures in Flutter async code.Final Answer:
Missing await keyword before putFile causing unhandled Future. -> Option CQuick Check:
Always await async upload calls [OK]
- Forgetting await on putFile()
- Confusing ref() usage
- Passing wrong file type without checking
List<File> images to Firebase Storage under folder 'gallery/'. Which approach correctly uploads all files and collects their download URLs?Solution
Step 1: Upload files sequentially with await
Uploading each file with await ensures each upload completes before moving on, preventing errors.Step 2: Get download URLs after each upload
After each upload, callgetDownloadURL()on the file's reference to get its URL and store it.Step 3: Collect URLs in a list
Store each URL in a list to use later, ensuring all files are uploaded and URLs are valid.Final Answer:
Use a loop with await inside to upload each file and then call getDownloadURL() for each upload, storing URLs in a list. -> Option AQuick Check:
Upload with await, then getDownloadURL() per file [OK]
- Not awaiting uploads causing race conditions
- Calling getDownloadURL() before upload
- Assuming one getDownloadURL() call returns all URLs
Ready to prove it?
Pass the test and this pattern moves unknown
