【问题标题】:Not able to save the url in firestore无法将网址保存在 Firestore 中
【发布时间】:2020-09-28 14:14:05
【问题描述】:

我正在使用下面的代码将flutter应用程序中选择的图像上传到firebase存储,这已成功完成,但我无法将图片的url保存在firestore中,在这种情况下我做错了什么?

您可以在下面的代码中看到存储 url 的尝试。请指导我应该如何纠正它?

Future uploadFile(String uid) async {

    StorageReference reference = FirebaseStorage.instance.ref().child('profile_pic/$uid');
    StorageUploadTask uploadTask = reference.putFile(avatarImageFile);
    StorageTaskSnapshot storageTaskSnapshot;
    uploadTask.onComplete.then((value) {
      if (value.error == null) {
        storageTaskSnapshot = value;
        storageTaskSnapshot.ref.getDownloadURL().then((downloadUrl) {
          setState(() {
            photoUrl = downloadUrl;

          });
          Firestore.instance
              .collection('users')
              .document(uid)
              .setData({ 'photoUrl': photoUrl}).then((data) async {
            setState(() {
              isLoading = false;
            });
            Fluttertoast.showToast(msg: "Upload success");
          }).catchError((err) {
            setState(() {
              isLoading = false;
            });
            Fluttertoast.showToast(msg: err.toString());
          });
        }, onError: (err) {
          setState(() {
            isLoading = false;
          });
          Fluttertoast.showToast(msg: 'This file is not an image');
        });
      } else {
        setState(() {
          isLoading = false;
        });
        Fluttertoast.showToast(msg: 'This file is not an image');
      }
    }, onError: (err) {
      setState(() {
        isLoading = false;
      });
      Fluttertoast.showToast(msg: err.toString());
    });
  }

【问题讨论】:

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


【解决方案1】:

我以与您相同的方式集成了上传过程并遇到了同样的问题,但使用 javascript 而不是在我的应用程序中进行。

我认为您没有下载网址,因为上传过程后没有承诺。

uploadTask.onComplete.then((value) {
      if (value.error == null) {
        storageTaskSnapshot = value;
        storageTaskSnapshot.ref.getDownloadURL().then((downloadUrl) {
          setState(() {
            photoUrl = downloadUrl;

          }); // <------- try to add a .then here 
          Firestore.instance
              .collection('users')
              .document(uid)
              .setData({ 'photoUrl': photoUrl}).then((data) async {
            setState(() {
              isLoading = false;
            });
            Fluttertoast.showToast(msg: "Upload success");

我标记了这条线,用then() 继续这个功能,这样你就有了承诺。还要确保使用awaits 并使用firebase 函数来异步上传进程,这样你就不会遇到奇怪的问题

【讨论】:

    【解决方案2】:

    该方法已被声明为异步,因此请尝试使用await 而不是then()

    Future uploadFile(String uid) async {
    
        StorageReference reference = FirebaseStorage.instance.ref().child('profile_pic/$uid');
        StorageTaskSnapshot snapshot = await reference.putFile(avatarImageFile).onComplete;
          if (snapshot.error == null) {
            final String photoUrl = await snapshot.ref.getDownloadURL();
               Firestore.instance
                  .collection('users')
                  .document(uid)
                  .setData({ 'photoUrl': photoUrl}).then((data) async {
                setState(() {
                  isLoading = false;
                });
                Fluttertoast.showToast(msg: "Upload success");
              }).catchError((err) {
                setState(() {
                  isLoading = false;
                });
                Fluttertoast.showToast(msg: err.toString());
              });
            }, onError: (err) {
              setState(() {
                isLoading = false;
              });
              Fluttertoast.showToast(msg: 'This file is not an image');
            });
          } else {
            setState(() {
              isLoading = false;
            });
            Fluttertoast.showToast(msg: 'This file is not an image');
          }
        }, onError: (err) {
          setState(() {
            isLoading = false;
          });
          Fluttertoast.showToast(msg: err.toString());
        });
      }
    

    现在使用await 时,您将能够先检索 url,然后将其保存在 Firestore 中。

    【讨论】:

      猜你喜欢
      • 2021-10-23
      • 2019-01-10
      • 2021-04-25
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多