skip to Main Content

I am currently developing a chat application which allows users to chat with another random user by clicking a button. I have added the following package to my app: https://docs.flyer.chat/flutter/firebase/firebase-usage

My database looks as follows:

collection: rooms
document: chatroom1
subcollection: messages

collection: users
document: uid1 (which then includes fields like username, gender etc.)

So now I do not know how to get two random users from my users collection and create a chat room for them, in order to allow them to chat. How can I code the matching? And how do I create a chatroom for each random match? Is there a simple way?

2

Answers


  1. First You need to create a list of users(uid or the doc_id) like this

    final list = ['user1', 'user2', 'user3', 'user4', 'user5'];
    

    then when you are trying to start chat so you can select the opponent by using this method

    // generates a new Random object
    
    final _random = new Random();
    
    // generate a random index based on the list length
    // and use it to retrieve the element
    
    var element = list[_random.nextInt(userList.length)];
    

    so it is just a reference for you for getting randow user.
    Hope you understand

    Login or Signup to reply.
  2. There is no simple way to do this. You will need to query your users collection to get a list of all users. Then, you will need to randomly select two users from that list and create a chat room for them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search