如何在 Flutter 代码中创建云存储图像 URL?

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

我有一个 'image' 存储在 Cloud Storage 的一个文件夹中,我想在新用户使用电子邮件和密码注册时将其用作默认图像。创建帐户后,用户可以选择添加自己的图像,但我希望所有用户都从默认图像开始。根据 SO 上的不同 questions/answers,Flutter 无法读取 Cloud Storage 本机使用的 (gs)URLs 来描述上传文件的位置。如何创建 Flutter 可以读取的 url?

我的云存储 URL 是: gs://购物-app.appspot.com/defaultUserImage/default_user_icon.jpg

在此先感谢您的帮助。

如果您使用默认存储桶,您可以获得 URL 如:

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

download URLs 上云存储的 FlutterFire 文档准确地涵盖了您的要求。从那里:

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);
}

您可以通过将 FirebaseStorage.instance.refFromURL() method.

传递给 gs:// URL 来获得 StorageReference