How to Get Download URL From Firebase in Flutter?
Earlier we have been through Step by Step guide to implement Firebase with Flutter so in this article, we will learn about How to Get a Download URL From Firebase In Flutter.
Are you as excited as us?
Let’s Discuss the same.
How to Get Download URL From Firebase In Flutter?
Firebase Storage is used to store images uploaded by the user from a Flutter Mobile Application. Users need to put
firebase_storage: ^10.0.0
in pubspec.yaml file and import package
import 'package:firebase_storage/firebase_storage.dart';
into the Dart File. Users can give try to below code.
Future<String> uploadImage(var imageFile ) async { Reference ref = storage.ref().child("/photo.jpg"); UploadTask uploadTask = ref.putFile(imageFile); var dowurl = await (await uploadTask.onComplete).ref.getDownloadURL(); url = dowurl.toString(); return url; }
URL looks like the below:
printUrl() async { Reference ref = FirebaseStorage.instance.ref().child("images/sky.jpg"); String url = (await ref.getDownloadURL()).toString(); print(url); }
User can create a function like the below:
Future<String>photoOption() async { try { DateTime now = new DateTime.now(); var datestamp = new DateFormat("yyyyMMdd'T'HHmmss"); String currentdate = datestamp.format(now); File imageFile = await ImagePicker.pickImage(); Reference ref = FirebaseStorage.instance .ref() .child("images") .child("$currentdate.jpg"); UploadTask uploadTask = ref.putFile(imageFile); Uri downloadUrl = (await uploadTask.future).downloadUrl; addUser.downloadablelink = downloadUrl.toString(); downloadableUrl = downloadUrl.toString(); print(downloadableUrl); } catch (error) { print(error); } return downloadableUrl; }
Future will look like the below:
Future<String> urlDownload(file) async { var uuid = Uuid().v1(); Reference ref = FirebaseStorage.instance.ref().child("post_$uuid.jpg"); UploadTask uploadTask = ref.putFile(file); String downloadUrl = await (await uploadTask.onComplete).ref.getDownloadURL(); return downloadUrl;}
Users can also give try to the below code.
Future mainprofile(File image) async { try { DateTime now = new DateTime.now(); var datestamp = new DateFormat("yyyyMMdd'T'HHmmss"); String currentdate = datestamp.format(now); _firebaseStorageRef = FirebaseStorage.instance .ref() .child(userRepository.uid) .child('main') .child("$currentdate.jpg"); UploadTask uploadTask = _firebaseStorageRef.putFile(image); uploadTask.onComplete.then((onValue) async { _mainurl = (await _firebaseStorageRef.getDownloadURL()).toString(); }); } catch (error) { print(error); } }
If you are
Future getImage() async { var image = await ImagePicker.pickImage(source: ImageSource.gallery); setState(() { _image = image; print('Image Path $_image'); }); }
then upload it.
Future uploadPic(BuildContext context) async { String fileName = basename(_image.path); Reference firebaseStorageRef = FirebaseStorage.instance.ref().child(fileName); UploadTask uploadTask = firebaseStorageRef.putFile(_image); StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete; final String url = (await taskSnapshot.ref.getDownloadURL()); print('URL Is $url'); }
Conclusion:
Thanks for being with us on a Flutter Journey !!!
Keep Learning !!! Keep Fluttering !!!
Still, Need a Support for Flutter Development?
We would love to assist you.
FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc.
FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.
locopump
What are the changes in flutter 2.0 with firebase_storage 8.1.3 to get the URL
Somyarajsinh jadeja
It does have any improvements in flutter 2.0 with firebase_storage 8.1.3 to get the URL
to accept this
– Update a dependency to the latest release.
– FIX: refFromUrl parse url (#6353).
more info Follow this link:- https://pub.dev/packages/firebase_storage/changelog