【问题标题】:Add document to collection in Cloud Firestore without overwrite previous document Flutter将文档添加到 Cloud Firestore 中的集合而不覆盖以前的文档 Flutter
【发布时间】:2020-11-22 08:01:10
【问题描述】:

在我的颤振应用程序中,我需要将文档添加到集合“influencerPost”中,并且每次用户添加新的“帖子”时,都必须将这个文档添加到之前的文档中......但是此时我只是覆盖了以前的文件。 如你所见,我有uploadImage 未来 它拾取图像并将其存储到 Firebase 中的存储。 之后我创建了一个名为 influencerPost 的集合,每次新帖子生成新文档时我都必须添加而不覆盖

这是完整的代码:

Future uploadImage(BuildContext context) async {
    String fileName = basename(_postImage.path);
    Reference firebaseStorageRef =
        FirebaseStorage.instance.ref().child('postImage/$fileName');
    UploadTask uploadTask = firebaseStorageRef.putFile(_postImage);
    TaskSnapshot taskSnapshot = await uploadTask.whenComplete(() => null);
    final String downloadUrl = await taskSnapshot.ref.getDownloadURL();
    await FirebaseFirestore.instance
        .collection('influencerPost')
        .doc(firebaseUser.uid)
        .set({
      "imageUrl": downloadUrl,
      'postText': IfUserProfile.post
    });
    setState(() => CircularProgressIndicator());
  }

通过这种方式,我得到了所有文件,但文件被覆盖,而不是我需要保留以前的文件并添加一个新文件。

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    由于您想针对用户维护多个帖子,因此您必须创建子集合。检查下面我添加子集合的代码 .collection("post")

        Future uploadImage(BuildContext context) async {
        String fileName = basename(_postImage.path);
        Reference firebaseStorageRef =
            FirebaseStorage.instance.ref().child('postImage/$fileName');
        UploadTask uploadTask = firebaseStorageRef.putFile(_postImage);
        TaskSnapshot taskSnapshot = await uploadTask.whenComplete(() => null);
        final String downloadUrl = await taskSnapshot.ref.getDownloadURL();
        await FirebaseFirestore.instance
            .collection('influencerPost')
            .doc(firebaseUser.uid)
            .collection("post")
            .add({
          "imageUrl": downloadUrl,
          'postText': IfUserProfile.post
        });
        setState(() => CircularProgressIndicator());
      }
    

    【讨论】:

    • 谢谢并从云火库中获取它需要指定影响者发布/发布路径对吗?
    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 2020-07-02
    • 2020-01-05
    • 2020-06-28
    • 2019-01-01
    • 2021-08-26
    • 1970-01-01
    相关资源
    最近更新 更多