【问题标题】:Can't Link Storage Firebase images, to Cloud Firestore correctly无法将 Storage Firebase 映像正确链接到 Cloud Firestore
【发布时间】:2021-03-04 01:57:04
【问题描述】:

我不明白为什么我无法链接并将 URL 发送到 Cloud Firestore 集合文档 图像在存储中正确拾取,但无法在 cloud firestore db 中获取 URL 这是我的代码:


  bool isLoading = false;
  var firebaseUser =  FirebaseAuth.instance.currentUser;


  Future uploadImage(BuildContext context) async {
    String fileName = basename(_imageFile.path);
    Reference firebaseStorageRef =
    FirebaseStorage.instance.ref().child('uploads/$fileName');
    UploadTask uploadTask = firebaseStorageRef.putFile(_imageFile);
    TaskSnapshot taskSnapshot = await uploadTask.whenComplete(() => null);
    if (taskSnapshot == null) {
      final String downloadUrl =
      await taskSnapshot.ref.getDownloadURL();
      await FirebaseFirestore.instance.doc(firebaseUser.uid)
          .collection("influncerUser")
          .add({"imageUrl": downloadUrl,});
      setState(() {
        isLoading = true;
      });
    }
  }

【问题讨论】:

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


    【解决方案1】:

    首先,为什么要使用 if (taskSnapshot == null) ?如果 taskSnapshot 为 null 则无法获取下载 url,所以删除它。

    其次,更喜欢使用onComplete 而不是 .whenComplete(() => null)

    Future uploadImage(BuildContext context) async {
    String fileName = basename(_imageFile.path);
    Reference firebaseStorageRef =
    FirebaseStorage.instance.ref().child('uploads/$fileName');
    UploadTask uploadTask = firebaseStorageRef.putFile(_imageFile);
    
    // use onComplete to await when the doc has been pushed.
    TaskSnapshot taskSnapshot = await uploadTask.onComplete;
    
    final String downloadUrl = await taskSnapshot.ref.getDownloadURL();
    await FirebaseFirestore.instance.doc(firebaseUser.uid)
        .collection("influncerUser")
        .add({"imageUrl": downloadUrl,});
    setState(() => isLoading = true);
    

    }

    【讨论】:

    • onComplete 似乎已被弃用,我在下面有一条红线,唯一可用的选项是 whenComplete。
    • 这两种方式都不会将URL添加到cloud firestore中的集合中。我也收到了这个错误:断言失败:第 129 行 pos 12:'isValidDocumentPath(documentPath)':文档路径必须指向有效文档。
    • 我不知道 onComplete 已被弃用。用户的路径似乎不正确,用户文档是存储在集合中还是存储在 Firestore 的根目录中?
    • E/flutter (3791): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] 未处理异常:'package:cloud_firestore/src/firestore.dart':断言失败:行129 pos 12:'isValidDocumentPath(documentPath)':文档路径必须指向有效文档。 E/flutter (3791): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39) E/flutter (3791): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart: 36:5) E/flutter (3791): #2 FirebaseFirestore.doc (package:cloud_firestore/src/firestore.dart:129:12)
    • 我在完成时删除了我保持原样:TaskSnapshot taskSnapshot = await uploadTask;但还是什么都没有
    猜你喜欢
    • 1970-01-01
    • 2020-08-15
    • 2021-03-03
    • 1970-01-01
    • 2020-11-01
    • 2021-12-25
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    相关资源
    最近更新 更多