【问题标题】:Flutter Firestore Storage get downloadUrlFlutter Firestore 存储获取 downloadUrl
【发布时间】:2022-01-07 16:55:54
【问题描述】:

我需要通过将照片上传到 Firebase 存储来获取 downloadURL,以便将其存储在 Firestore 文档中。我的代码的问题是保存的 URL 不是 https//:所以我需要获取 downloadURL。我想知道我需要在哪里调用它来获取 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);
    }
  }

【问题讨论】:

    标签: firebase flutter google-cloud-firestore firebase-storage


    【解决方案1】:

    文件上传完成后,您可以随时在参考上调用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,
    });
    

    【讨论】:

    • 图片上传正常,但参考没有更新
    • 我有一个小错字(两个.. 不小心)。如果这不是原因,您能否更明确地说明问题出在哪里。例如:在调试器中单步执行代码,查看在调用getDownloadURL() 之后downloadURL 的值。你觉得这样对吗?
    猜你喜欢
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2021-07-23
    • 2020-03-28
    • 2019-09-01
    • 2021-03-18
    • 1970-01-01
    相关资源
    最近更新 更多