13

I correctly receive UploadTaskSnapshot, and the field downloadUrl contains an instance of Uri that parses download link of uploaded file.

How to get storage and downloadUrl as strings?

5 Answers 5

17

old

final uploadTask = imageStore.putFile(imageFile);
final url = (await uploadTask.future).downloadUrl;

update

This answer https://stackoverflow.com/a/52690889/217408 is now the accurate one.

final ref = FirebaseStorage.instance
    .ref()
    .child('path')
    .child('to')
    .child('the')
    .child('image_filejpg');

ref.putFile(imageFile);
// or ref.putData(Uint8List.fromList(imageData));

var url = await ref.getDownloadURL() as String;

or

var url = Uri.parse(await ref.getDownloadURL() as String);
14
  • How to get string url out of that URI that downloadUrl returns.
    – Tree
    May 18, 2018 at 6:15
  • var urlString = url.toString() or var urlString = '${url}' May 18, 2018 at 6:15
  • Uri parses firebase tokens ect.. and I dont know how to get full url. Also, that only answers half of the question. Is there a way to get storage location?
    – Tree
    May 18, 2018 at 6:16
  • Thank you, though
    – Tree
    May 18, 2018 at 6:16
  • Why would you need the storage location? That's what you need to have already before you can upload (imageStore.path) May 18, 2018 at 6:18
7

I get downloadUrl from v1.0.3 by the following code.

StorageReference storageReference = _storage.ref().child(path);
StorageUploadTask uploadTask = storageReference.putFile(imageFile);

StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;

String downloadUrl = await taskSnapshot.ref.getDownloadURL();
0
6

@DomingoMG it looks like with the latest release they want:

String location = await ref.getDownloadURL();

See https://pub.dartlang.org/packages/firebase_storage#-example-tab-

2
  • What is the ref please explain better, I'm having issues getting the Download URL.
    – Tayo.dev
    Oct 8, 2018 at 16:19
  • StorageReference. Go to link and search getDownloadURL
    – ezaspi
    Oct 8, 2018 at 22:53
2

Update: Nov 2020

onComplete is now removed from the upload task. So, use:

var reference = FirebaseStorage.instance.ref().child("your_path");
await reference.putFile(fileToUpload);
var url = await reference.getDownloadURL();
1
  • how about getting a list of multi uploaded images?
    – griffins
    Jan 31, 2020 at 19:18
0

With the version of firebase_storage: ^11.0.11. It worked this way:

final FirebaseFirestore firestore = FirebaseFirestore.instance;
  final FirebaseStorage storage = FirebaseStorage.instance;

  DocumentReference get firestoreRef => firestore.doc('products/$id');
  Reference get storageRef => storage.ref().child('products').child(id!);
final UploadTask task = storageRef.child(const Uuid().v1()).putFile(newImage as File);
    final TaskSnapshot snapshot = await task.whenComplete(() {});
    final String url = await snapshot.ref.getDownloadURL();
    updateImages.add(url);

OBS: I'm using package uuid: ^3.0.7 to create a random id for each upload

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.