Getting the URL from FirebaseStorage URI in Flutter
According to the documentation, the Uri class has a method toString().
return url.toString();
Comments
-
Zenko over 1 year
Sorry for the noob question. But how do I get the Url String from a Uri ? In my understanding the Url is a subset of Uri but I couldn't find any method that says something like
.getUrl
. Did I miss anything ?Background:
ThegetDownloadURL()
of FirebaseStorage in Flutter Web somehow returnsUri
instead ofString
.The explanation from from Flutter Web's FirebaseStorage package:
Future<Uri> getDownloadURL() package:firebase/src/storage.dart
Returns a long lived download URL for this reference.
The
getDownloadURL
explanation from from Flutter App's FirebaseStorage package:Future<String> getDownloadURL() package:firebase_storage/firebase_storage.dart
Fetches a long lived download URL for this object.
In the Flutter App my pubspec.yaml:
firebase_core: ^0.5.1 firebase_auth: ^0.18.2 cloud_firestore: ^0.14.2 firebase_storage: ^5.0.0-dev.4
In the Flutter Web my pubspec.yaml:
firebase: ^7.3.2 cloud_firestore: ^0.14.2 cloud_functions: ^0.6.0+1
I tried to find solutions and read the docs but I haven't found any writing about converting the Uri to Url from Firebase Storage
getDownloadURL
method. It seems to be treated almost like the same thing. But it gave me error. See Code below:*** Code:
Future<String> uploadImage(String localFilename, String filename, String path, StorageReference ref) async { final byteData = await rootBundle.load('assets/posts_images/$localFilename'); final bytes = byteData.buffer.asUint8List(); final metadata = UploadMetadata(contentType: 'image/jpeg'); final uploadTask = ref.child('$path/$filename').put(bytes, metadata); final snapshot = await uploadTask.future; final url = await snapshot.ref.getDownloadURL(); return url; // Error: A value of type 'Uri' can't be returned from method 'uploadImage' because it has a return type of 'Future<String>' }
Thanks
-
Doug Stevenson over 3 yearsPlease edit the question to show the code that isn't working the way you expect.
-
Zenko over 3 yearsThanks for the comment. I have added the code above. And this is only in Flutter Web. In Flutter App this doesn't happen.
-
Zenko over 3 yearsBut my question is how to convert the URI to URL and possibly to get answer on how to get the URL from the Firebase Storage methods in Flutter Web
-
-
Zenko over 3 yearsOh it is that simple. I didn't realize it, I thought the
.toString
would just return something likeA Uri Object
or something else. Didn't realize it actually turns it into Url. Thanks Doug. Sorry for the noob question again..