【问题标题】:Documents in Cloud Firestore is not showing up in query result in flutterCloud Firestore 中的文档未显示在 Flutter 的查询结果中
【发布时间】:2021-11-04 16:30:15
【问题描述】:

  void onSendMessage(String content) {
    
    if (content.trim() != '') {
      textEditingController.clear();
      var documentReference = FirebaseFirestore.instance
          .collection('messages')
          .doc(widget.comid)
          .collection(widget.comid)
          .doc(DateTime.now().millisecondsSinceEpoch.toString());

      FirebaseFirestore.instance.runTransaction((transaction) async {
        transaction.set(
          documentReference,
          {
            'idFrom': widget.userid,
            'idTo': widget.peerid,
            'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
            'content': content,
            
          },
        );
      });
    }
    else {
      Fluttertoast.showToast(
        msg: 'Nothing to send', 
        backgroundColor: Colors.black, 
        textColor: Colors.grey[100]
      );
    }
  }

我想获取那些文档 ID,但它没有进入查询。但是,当我通过 Firestore 手动添加“Anakans”时,它会显示出来。

我是新来的 Flutter 和 Firestore,我犯了错误

已解决 我将文档字段设置为最近消息的最大价值。因此,该文件不再是一个不存在的祖先。我可以获取 id 列表。现在我的onSendMessage 函数看起来像

  void onSendMessage(String content) {
    if (content.trim() != '') {
      textEditingController.clear();

      var msg = {
        'idFrom': widget.userid,
        'idTo': widget.peerid,
        'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
        'content': content,
      };

      FirebaseFirestore.instance
          .collection('messages')
          .doc(widget.comid)
          .set(msg);

      var documentReference = FirebaseFirestore.instance
          .collection('messages')
          .doc(widget.comid)
          .collection(widget.comid)
          .doc(DateTime.now().millisecondsSinceEpoch.toString());

      FirebaseFirestore.instance.runTransaction((transaction) async {
        transaction.set(
          documentReference,
          {
            'idFrom': widget.userid,
            'idTo': widget.peerid,
            'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
            'content': content,
          },
        );
      });
    } else {
      Fluttertoast.showToast(
          msg: 'Nothing to send',
          backgroundColor: Colors.black,
          textColor: Colors.grey[100]);
    }
  }

【问题讨论】:

  • onSendMessage 是否用于添加您希望在查询中获得的消息?然后贴出你的实际代码(不是代码截图)
  • 请提供更多信息将很乐意提供帮助)以及为什么交易不只是设置?
  • @PeterO。 onSendMessage 在创建此文档的用户之间添加消息引用 ``` var documentReference = FirebaseFirestore.instance .collection('messages') .doc(widget.comid) .collection(widget.comid) .doc(DateTime.now( ).millisecondsSinceEpoch.toString()); ``` 然后我使用事务添加它
  • 我很好奇这里的 .doc(widget.comid).collection(widget.comid) 是什么?
  • 检查我的答案,你可以使用它。

标签: firebase flutter dart google-cloud-firestore stream-builder


【解决方案1】:

我刚刚运行了您的代码。我已经看到了这个问题。

onSendMessage 将文档添加到此路径messages/widget.comid/widget.comid/DateTime.now().millisecondsSinceEpoch.toString()

onSendMessage 没有向messages 集合添加任何文档,因此消息集合实际上是空的。相反,它向此路径messages/widget.comid/widget.comid/DateTime.now().millisecondsSinceEpoch.toString() 添加了一个文档。所以messages/widget.comid 不存在,因为您没有向messages 添加任何文档。

在firestore中,即使父文档不存在,子文档也可以存在。

所以“4ePoQ...”作为一个文档,不存在,但它有一个孩子(因为你定义了路径)。不存在的文档以斜体显示(您可以从 Firestore 屏幕截图中看到)。当您手动添加ankana 时,您正在向消息集合添加一个文档。

解决方案。您的流构建器应该监听子集合widget.comid。此集合包含文档(id 为DateTime.now().millisecondsSinceEpoch.toString())。

return Scaffold(
  appBar: AppBar(
    title: Text('Title'),
  ),
  body: StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
    stream: FirebaseFirestore.instance
        .collection('messages')
        .doc(widget.comid)
        .collection(widget.comid)
        .snapshots(),
    builder: (BuildContext context,
        AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
      if (snapshot.hasError) return Text('Something went wrong');
      if (snapshot.connectionState == ConnectionState.waiting)
        return CircularProgressIndicator();

      if (snapshot.data.docs.isNotEmpty) {
        print('document exists');
        print(snapshot.data.docs.map((e) => e.data()));
        return Container();
      } else {
        print(snapshot.data.docs);
        print('document does not exist');
        return Container();
      }
    },
  ),
);

【讨论】:

  • 感谢您解决问题,非常感谢。当我想要该文档 ID 时,我的应用程序设计没有 widget.comid。实际上那个 widget.comid 是文档 ID。我想要一次列出所有文档 ID 并将其显示到屏幕上。所以,我需要一种方法来避免放置文档路径并创建 document_id 然后创建集合,以便我以后可以访问该列表。
  • 好的,为此,让onSendMessage 将文档添加到messages/doc_id。然后你按照你现在正在做的方式来获取它。我认为您不需要时间戳作为 doc_id,因为您没有使用它进行获取。
猜你喜欢
  • 1970-01-01
  • 2020-07-02
  • 1970-01-01
  • 2021-07-12
  • 2023-03-17
  • 2023-03-30
  • 2020-07-12
  • 1970-01-01
  • 2018-11-28
相关资源
最近更新 更多