繁体   English   中英

Flutter Firestore 存储获取 downloadUrl

[英]Flutter Firestore Storage get downloadUrl

我需要通过将照片上传到 Firebase 存储来获取下载 URL,以便将其存储在 Firestore 文档中。 我的代码的问题是保存的 URL 不是 https//:所以我需要获取下载 URL。 我想知道我需要在哪里调用它来获取 downloadUrl 并将其保存在我的 Firestore 数据库中。

这是我的代码:

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

文件上传完成后,您可以随时在引用上调用getDownloadURL() 所以这将是一个好地方:

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