title: How to arrange cloud firestore documents in serial wise with flutter?
body: <p>I'm trying to create chat demo app with firebase in flutter but when i send message, then those message documents are being created at randomly any places in firestore database. thats why my chat screen messages are in wrong manner, means not arranged according to time.</p>
  
  <p><strong>Some piece of code</strong>:</p>
  
  <p>method for saving message details to firestore:</p>
  
  <pre><code>Future&lt;void&gt; sendMesssage() async{
     if(messagesController.text.length&gt;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();
  
     }
    }
  </code></pre>
  
  <p><strong>UI For chat screen:</strong></p>
  
  <p>method for fetching messages from firestore:</p>
  
  <pre><code>Expanded(
    child: StreamBuilder&lt;QuerySnapshot&gt;(
      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&lt;DocumentSnapshot&gt; 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'],
                          );
  
                        },
                       ),
                     );
                    }
                  },
                ),
              ),
  
  </code></pre>
  
  <p>chat Screen:</p>
  
  <p><a href="https://i.stack.imgur.com/ArddN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ArddN.png" alt="enter image description here"></a></p>
  
  <p>My Cloud Firestore Screenshot:</p>
  
  <p><a href="https://i.stack.imgur.com/14Kna.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/14Kna.png" alt="enter image description here"></a></p>