-5

I have created a chat with flutter and I need to use into my app, where 1group of user will chat with another kind of user group. At the moment my chat is developed in this manner and I create record in firebase so:

CollectionReference addMessage = FirebaseFirestore.instance.collection('Messages');
  Future<void> createMessageRecord() {
    return addMessage
        .add(
          { 'id': firebaseUser.uid,
            'text': textMessage,
            'timeStamp': DateTime.now().millisecondsSinceEpoch.toString(),
          },
        )

I retrieve them:

StreamBuilder(
        stream: FirebaseFirestore.instance.collection('Messages').orderBy('timeStamp', descending: true).snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData)
            return Center(child: CircularProgressIndicator());

I display them:

ListView.builder(
                        reverse: true,
                        padding: EdgeInsets.only(top: 15.0),
                        itemCount: snapshot.data.docs.length,
                        itemBuilder: (BuildContext context, int index) {
                          final message = snapshot.data.docs[index];
                          final bool isMe = message['id'] == firebaseUser.uid;
                          return _buildMessage(message, isMe);

the chat works pretty good but all users of course will have the same chat. What I want to do is limit the chat displaying just for the 2 users. the first one who starts the chat and and second one who get. At the moment I have created into firestore a collection called "Messages" and into I have the 3 documents id text timeStamp``` I dont know how to limit the displaying data just for this 2 users

Agos Dev
  • 1
  • 1
  • 4
  • You definitely don't want to use firestore for a chat app. Try using firebase realtime database. It is meant for that use case and is cheaper in the long run. Have a look at https://stackoverflow.com/questions/52410826/how-to-model-a-chat-application-on-firebase-realtime-database on how to structure your chat app. – CoderUni Dec 31 '20 at 16:56

1 Answers1

0

Instead of having one single document for all of the messages create a document per conversaiton this way you will only retrive the corresponding messages.

Also as mentioned on the comment by @Uni you shoudl be using realtime database also you can take a look at several examples on the web on how to accomplish that. for example

Louis C
  • 645
  • 5
  • 12
  • Could you make me an example please "Instead of having one single document for all of the messages create a document per conversaiton this way you will only retrive the corresponding messages." – Agos Dev Jan 01 '21 at 14:56
  • Could you explaine me how to create multiple document per single collection? – Agos Dev Jan 02 '21 at 14:52
  • On the [firebase docs](https://firebase.google.com/docs/firestore/data-model#hierarchical-data) it's a clear example on how to structure the data on a chat app example. – Louis C Jan 04 '21 at 18:44
  • Also keep in mind that the purpose of stakcoverflow is to solve question, please avoid making comments like "Could you make me an example" – Louis C Jan 04 '21 at 18:46