简体   繁体   English

Flutter Firestore 存储获取 downloadUrl

[英]Flutter Firestore Storage get downloadUrl

I need to get the downloadURL from uplaoding a photo to a Firebase Storage so I can store it inside of a Firestore document.我需要通过将照片上传到 Firebase 存储来获取下载 URL,以便将其存储在 Firestore 文档中。 The issue with my code is that the URL that is saved isnt a https//: so I need to get the downloadURL.我的代码的问题是保存的 URL 不是 https//:所以我需要获取下载 URL。 I was wondering where I need to call it to get the downloadUrl and save it inside of my Firestore Database.我想知道我需要在哪里调用它来获取 downloadUrl 并将其保存在我的 Firestore 数据库中。

Here is my code:这是我的代码:

  Future<void> _uploadProfilePhoto(String inputSource) async {
    final picker = ImagePicker();
    PickedFile? pickedImage;
    try {
      pickedImage = await picker.getImage(
          source: inputSource == 'camera'
              ? ImageSource.camera
              : ImageSource.gallery,
          maxWidth: 1920);

      final String fileName = path.basename(pickedImage!.path);
      File imageFile = File(pickedImage.path);

      try {
        await storage.ref("avatars/$fileName").putFile(
            imageFile,
            SettableMetadata(customMetadata: {
              'uploaded_by': '$uid',
            }));
            
        // Create/Update firesotre document
        users.doc(uid).update({
          "profilePhoto": fileName,
        });

        setState(() {});
      } on FirebaseException catch (error) {
        print(error);
      }
    } catch (err) {
      print(err);
    }
  }

You can call getDownloadURL() on the reference at any time after the file upload has completed.文件上传完成后,您可以随时在引用上调用getDownloadURL() So this would be a good spot:所以这将是一个好地方:

await storage.ref("avatars/$fileName").putFile(
    imageFile,
    SettableMetadata(customMetadata: {
      'uploaded_by': '$uid',
    }));
var downloadURL = await storage.ref("avatars/$fileName")..getDownloadURL();       
// Create/Update firesotre document
users.doc(uid).update({
  "profilePhoto": downloadURL,
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM