Getting the URL from FirebaseStorage URI in Flutter

2,544

According to the documentation, the Uri class has a method toString().

return url.toString();
Share:
2,544
Zenko
Author by

Zenko

Creating apps for a living and for fun!

Updated on December 25, 2022

Comments

  • Zenko
    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:
    The getDownloadURL() of FirebaseStorage in Flutter Web somehow returns Uri instead of String.

    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
      Doug Stevenson over 3 years
      Please edit the question to show the code that isn't working the way you expect.
    • Zenko
      Zenko over 3 years
      Thanks for the comment. I have added the code above. And this is only in Flutter Web. In Flutter App this doesn't happen.
    • Zenko
      Zenko over 3 years
      But 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
    Zenko over 3 years
    Oh it is that simple. I didn't realize it, I thought the .toString would just return something like A Uri Object or something else. Didn't realize it actually turns it into Url. Thanks Doug. Sorry for the noob question again..