'Get Download URL from Firebase Storage in Flutter

I'm currently exploring Flutter, I found there is an official Firebase Storage plugin in Flutter firebase_storage I have storage reference like this one:

final StorageReference ref = FirebaseStorage.instance.ref().child("default.png");

But there is no method to get download URL from that StorageReference.



Solution 1:[1]

If the above solution doesn't work, try this:

Future<String> uploadImage(var imageFile ) async {
    StorageReference ref = storage.ref().child("/photo.jpg");
    StorageUploadTask uploadTask = ref.putFile(imageFile);

    var dowurl = await (await uploadTask.onComplete).ref.getDownloadURL();
    url = dowurl.toString();

    return url; 
}

Solution 2:[2]

use url like this:

printUrl() async {
    StorageReference ref = 
        FirebaseStorage.instance.ref().child("images/sky.jpg");
    String url = (await ref.getDownloadURL()).toString();
    print(url);
}

Solution 3:[3]

I had Implemented a method to save your image with a timestamp and get the downloadable url.

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();


        StorageReference ref = FirebaseStorage.instance
            .ref()
            .child("images")
            .child("$currentdate.jpg");
        StorageUploadTask 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;
}

Solution 4:[4]

At latest version firebase_storage: ^5.0.1 Approach should be like below:

Reference reference = FirebaseStorage.instance.ref().child("SupportChatImages").child(fileName);

    UploadTask uploadTask =  reference.putFile(imageFile);

    uploadTask.whenComplete(() async{

      try{
        imageUrl = await reference.getDownloadURL();
      }catch(onError){
        print("Error");
      }

      print(imageUrl);

    });

Solution 5:[5]

Future<String> urlDownload(file) async {
var uuid = Uuid().v1();
StorageReference ref =
    FirebaseStorage.instance.ref().child("post_$uuid.jpg");
StorageUploadTask uploadTask = ref.putFile(file);

String downloadUrl =
    await (await uploadTask.onComplete).ref.getDownloadURL();
return downloadUrl;}

Solution 6:[6]

For firebase_storage: ^10.0.1

Here is the code to get URL of Uploaded Image..

uploadImagetFirebase(String imagePath) async {
 await FirebaseStorage.instance
  .ref(imagePath)
  .putFile(File(imagePath))
  .then((taskSnapshot) {
print("task done");

// download url when it is uploaded
if (taskSnapshot.state == TaskState.success) {
  FirebaseStorage.instance
      .ref(imagePath)
      .getDownloadURL()
      .then((url) {
    print("Here is the URL of Image $url");
    return url;
  }).catchError((onError) {
    print("Got Error $onError");
  });
}
});
}

Solution 7:[7]

He is my solution :

StorageReference storageReference = FirebaseStorage.instance.ref().child("myfile"); 
StorageUploadTask uploadTask = storageReference.putFile(file);
uploadTask.onComplete.then((s){ 
   s.ref.getDownloadURL(); 
});

Solution 8:[8]

My Solution

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");
      StorageUploadTask uploadTask = _firebaseStorageRef.putFile(image);
      uploadTask.onComplete.then((onValue) async {
        _mainurl = (await _firebaseStorageRef.getDownloadURL()).toString();
      });
    } catch (error) {
      print(error);
    }
  }

Solution 9:[9]

I have tried many ways and this worked for me after many tries as Firebase Storage removing old methods. If anyone getting error of 404 Object not found Then the below code also solves that.

Future<String> uploadSingleImage(File file) async {
    //Set File Name
    String fileName = DateTime.now().millisecondsSinceEpoch.toString() +
        AuthRepository.getUser().uid +
        '.jpg';
    
    //Create Reference
    Reference reference = FirebaseStorage.instance
        .ref()
        .child('Single Post Images')
        .child(fileName);

    //Now We have to check the status of UploadTask
    UploadTask uploadTask = reference.putFile(file);
    
    String url;
    await uploadTask.whenComplete(() async {
      url = await uploadTask.snapshot.ref.getDownloadURL();
    });
   
    return url;
  }

Solution 10:[10]

try this

 Future<String> downloadFromFirebase() async {
  // Create reference
    StorageReference ref = FirebaseStorage.instance.ref().child("default.png");
    String _myUrl = await ref.getDownloadURL();
    return _myUrl.toString();
}

Solution 11:[11]

Here is my solution

this part is how i get image from picker

 Future getImage() async {
  var image = await ImagePicker.pickImage(source: ImageSource.gallery);

  setState(() {
    _image = image;
      print('Image Path $_image');
  });
}

than i upload it

 Future uploadPic(BuildContext context) async {

  String fileName = basename(_image.path);
  StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(fileName);
  StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
  StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;


  final String url = (await taskSnapshot.ref.getDownloadURL());
  print('URL Is $url');
}

hope it will help someone

Solution 12:[12]

Here is my approach, to upload an image to Firebase Storage and get the dowlad URL

 var img_name=DateTime.now().millisecondsSinceEpoch.toString()+".png";

 final StorageReference storageReference = FirebaseStorage.instance.ref().child("images/profile/"+img_name);

 var upload= await storageReference.putFile(croppedFile);
 await upload.onComplete;

 var url=await storageReference.getDownloadURL();
 print(url.toString());

Solution 13:[13]

**Solution for latest firebase_storage 9.0.0 **

Future<void> _uploadImage() async {
if (_image != null) {
  final fileName = '${DateTime.now()}.jpeg';
  Reference reference = _firebaseStorage.ref('uploads/$fileName');
  TaskSnapshot taskSnapshot = await reference
      .putFile(_image!)
      .whenComplete(() => reference.getDownloadURL());

  print(taskSnapshot.ref.fullPath.toString());

  setState(() {
    _imageUploadState = ImageUploadState.done;
  });
}

}

Solution 14:[14]

For firebase_storage: ^10.0.1

import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;

String image = 'gs://ID.appspot.com/image/1026_800x800.jpg';

firebase_storage.FirebaseStorage.instance.refFromURL(image).getDownloadURL().then((url) async {
                  print(url);
                });

Will download something like this: https://firebasestorage.googleapis.com/v0/b/ID.appspot.com/o/...

Solution 15:[15]

Try This

Future<String> uploadFile(XFile _file) async {
File file = File(_file.path);
try {
  var ref = FirebaseStorage.instance.ref('uploads/' + _file.name);
  var uploadTask = await ref.putFile(file);
  if (uploadTask.state == TaskState.success) {
    final url = await ref.getDownloadURL();
    return url;
  }
  return "0";
} catch (e) {
  print(e);
  return e.toString();
  }
}

Solution 16:[16]

Future<String> uploadImage(imageFile) async {
Reference ref = FirebaseStorage.instance.ref().child('default.png');
UploadTask uploadTask = ref.putFile(imageFile);
final snapshot = await uploadTask.whenComplete(() => {});
final urlDownload = await snapshot.ref.getDownloadURL();
print("The url is here! $urlDownload");
return urlDownload;

}

Solution 17:[17]

My solution using Async/await syntax and newest Firebase Storage 2022

Future<String> uploadFileToStorage(String path, File image) async {
    TaskSnapshot uploadTask = await _storageReference.child(path).putFile(image);

    String pathdownlaod = await uploadTask.ref.getDownloadURL();

    return pathdownlaod;
  }