【问题标题】:How to arrange cloud firestore documents in serial wise with flutter?如何用颤振连续排列云存储文件?
【发布时间】:2020-05-03 23:48:57
【问题描述】:

我正在尝试在颤振中创建带有 firebase 的聊天演示应用程序,但是当我发送消息时,这些消息文档会在 firestore 数据库中的任何位置随机创建。这就是为什么我的聊天屏幕消息的方式不对,意味着没有按时间安排。

一些代码

将消息详细信息保存到 Firestore 的方法:

Future<void> sendMesssage() async{
   if(messagesController.text.length>0){
     String msgId = firestore.collection("messages").document().documentID.toString();

     await firestore.collection("messages").document(msgId).setData({
       'text': messagesController.text,
       "from": widget.user.user.email, //sender email Id
       "to":widget.chatMateEmail, // receiver email id
       "msgId":msgId,
       "senderUid": widget.user.user.uid, //sender uid
       "receiverUid":widget.receiverUid //receiver uid
     });
     messagesController.clear();

   }
  }

聊天界面的用户界面:

从firestore获取消息的方法:

Expanded(
  child: StreamBuilder<QuerySnapshot>(
    stream: firestore.collection("messages").snapshots(),
    builder: (context, snapshot){
      if(snapshot.hasError){
          return Center(child: Text("${snapshot.error}"),);
        }
      if(!snapshot.hasData){
          return Center(child: CircularProgressIndicator(),);
        }else{
               List<DocumentSnapshot> docs = snapshot.data.documents;
                return
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: ListView.builder(
                    itemCount: docs.length,
                    itemBuilder: (context, index){
                        print("Messagessssssss:${docs[index]['text']}");
                        return Message( // custom class
                          from:  docs[index]['from'],
                          text: docs[index]['text'],
                          me: widget.user.user.email == docs[index]['from'],
                        );

                      },
                     ),
                   );
                  }
                },
              ),
            ),

聊天屏幕:

我的 Cloud Firestore 屏幕截图:

【问题讨论】:

  • Firestore 默认不按时间顺序对文档进行排序。您需要在文档中添加时间戳字段,并按该字段对查询进行排序。
  • @DougStevenson 先生,时间戳表示日期时间..!请您以代码形式添加您的评论吗?
  • 非常感谢先生,按照您的建议解决了:)

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

根据@DougStevenson先生的回答解决了,我添加了名为“messageTime”的新字段并添加了DateTime.now(),并根据messageTime获取消息(按升序排序)。

我修改了一些代码并完美运行:

 Future<void> sendMesssage() async{
   if(messagesController.text.length>0){
     String msgId = firestore.collection("messages").document().documentID.toString();

     await firestore.collection("messages").document(msgId).setData({
       ..........
       "messageTime": DateTime.now() // message DateTime
     });
     messagesController.clear();

   }
  }
Expanded(
  child: StreamBuilder<QuerySnapshot>(
    stream: firestore.collection("messages").orderBy('messageTime', descending: false).snapshots(), //and sort by ascending order according to datetime.
    builder: (context, snapshot){
            ......

              },
           ),
         ),

【讨论】:

    猜你喜欢
    • 2019-06-07
    • 2021-04-25
    • 1970-01-01
    • 2020-08-07
    • 2021-08-17
    • 2019-10-29
    • 2019-07-06
    • 2021-04-13
    • 2021-10-02
    相关资源
    最近更新 更多