1

I am using the following database structure.

enter image description here

Which is the same as presented in the official documentation

  // Conversation members are easily accessible
  // and stored by chat conversation ID
  "members": {
    // we'll talk about indices like this below
    "one": {
      "ghopper": true,
      "alovelace": true,
      "eclarke": true
    },
    "two": { ... },
    "three": { ... }
  },

I want to check if there is already a chat with those users so they don't create duplicated.

This is what i'm trying

final snapshot = await _database.ref().child("members").child(firstUserId).get();

but this is not finding anything because the chatId is first. Is there a way to do this or a better database structure?

I hope you can help me. Thanks in advance.

Joaquín Varela
  • 329
  • 5
  • 18
  • Have you read the [answer](https://stackoverflow.com/a/73910285/5246885) from an earlier answer of mine? – Alex Mamo Sep 30 '22 at 15:00

1 Answers1

0

I found a solution, I was creating a chat with the members first. I should create a user with the contact list and then check it like this:

  Future<bool> createChat(String firstUserId, String secondUserId) async {
    //Check if theres already a chat between the two users
    final snapshot = await _database.ref().child("users/$firstUserId/contacts").get();
    final userContacts = Map<String, dynamic>.from(snapshot.value as Map);
    //Check if the user is already in the contact list
    if (userContacts.containsKey(secondUserId)) {
      return false;
    }
}
Joaquín Varela
  • 329
  • 5
  • 18