Home > database >  How to create a Cloud Storage image URL in Flutter code?
How to create a Cloud Storage image URL in Flutter code?

Time:01-16

I have an 'image' stored in a folder in Cloud Storage that I want to use as a default image whenever new users sign up using email and password. Users will have the option to add their own image once their account is created but I want all users to start with a default image. Flutter, according to different questions/answers on SO, can't read the (gs)URLs that Cloud Storage uses natively to describe the location of an uploaded file. How do I create a url that Flutter can read?

My Cloud Storage URL is: gs:// shopping-app.appspot.com/defaultUserImage/default_user_icon.jpg

Thanks in advance for any help.

CodePudding user response:

If you use default bucket, you can get an URL like:

await FirebaseStorage.instance.ref('<file>').getDownloadURL();

CodePudding user response:

The FlutterFire documentation for Cloud Storage on download URLs covers precisely what you're asking. From there:

In many use-cases, you may wish to display images stored on a storage bucket within your application, using Firebase as a scalable and cost-effective Content Distribution Network (CDN). Firebase allows you to retrieve a long-lived download URL which can be used. To request a download URL, call the getDownloadURL method on a reference:

Future<void> downloadURLExample() async {
  String downloadURL = await firebase_storage.FirebaseStorage.instance
     .ref('users/123/avatar.jpg')
      .getDownloadURL();

  // Within your widgets:
  // Image.network(downloadURL);
}

You can get a StorageReference for the gs:// URL you have by passing it to the FirebaseStorage.instance.refFromURL() method.

  •  Tags:  
  • Related