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?

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Tree
  • 29,135
  • 24
  • 78
  • 98

5 Answers5

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);
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • How to get string url out of that URI that downloadUrl returns. – Tree May 18 '18 at 06:15
  • `var urlString = url.toString()` or `var urlString = '${url}'` – Günter Zöchbauer May 18 '18 at 06: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 '18 at 06:16
  • Thank you, though – Tree May 18 '18 at 06:16
  • Why would you need the storage location? That's what you need to have already before you can upload (`imageStore.path`) – Günter Zöchbauer May 18 '18 at 06:18
  • because as a path I send only relative path, so looks like I need to hardcode base url in the app. I did it that way in the end. – Tree May 18 '18 at 06:19
  • Path relative to what? I don't get what the actual problem is you're trying to solve. – Günter Zöchbauer May 18 '18 at 06:20
  • when you upload the file you do ` StorageUploadTask putFile = storage.ref().child("region/$fileName").putFile(_regionImage); ` but the actual storage path is `"gs://app-db.appspot.com/region/$fileName";` Just thought that I can avoid hardcoding this url. Uri solution worked so thank you – Tree May 18 '18 at 06:22
  • When using it, it indicates the following: The getter 'future' isn't defined for the class StorageUploadTask – DomingoMG Oct 07 '18 at 14:41
  • @DomingoMG try `flutter clean`. Otherwise I don't know. Doesn't look like the code I posted can produce such an error. – Günter Zöchbauer Oct 07 '18 at 16:17
  • See new answer below – ezaspi Oct 07 '18 at 17:10
  • @GünterZöchbauer: is there any ways to get all file in folder by using flutter ? So far, the answer which I know is NO. I need to get all files by file's name which was stored before. Thanks a lot! – Mr Special Oct 03 '19 at 09:09
  • I don't think so. As far as I know this is also not possible in other languages. This is a Firebase limitation (not sure if this limitation was fixed in Firebase since). When I run into that I used insert and delete triggers with cloud functions to create/delete Firebase database records that point to files to be able to query. – Günter Zöchbauer Oct 03 '19 at 19:18
  • The above way has been deprecated, to get the `url` check this answer: https://stackoverflow.com/a/64764390/7015400 – Peter Haddad Nov 10 '20 at 08:22
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();
ko2ic
  • 1,977
  • 14
  • 20
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-

ezaspi
  • 684
  • 7
  • 25
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();
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
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