I am developing a flutter app with Firebase Realtime Database facility. This is kind of a chat app. Here i have a root node called chat_room where it will save all chat rooms. Each chat_room will hold information about its members, the people who are in the chat. Below is my data structure now
My is requirement is, when I search by the email, I should be able to get all chat_rooms associated to that email. For an example, i need to know all chat_rooms where the email ID someone@test.com is registered with.
The issue is, i am now aware that I can't do that with my current structure, where I save data as an array. Reading this i got to know that using SET is a better alternative to Array as it we are allowed to perform such searches. So maybe this is the structure I need.
 chat_room
      chatroom1
        chat_mode: "supplier",
        last_message: "test",
        timestamp: 1223334
        members:
            email1: true
            email2: true
      chatroom2
        chat_mode: "supplier",
        last_message: "test",
        timestamp: 1223334
        members:
            email1: true
            email2: true
Below is the code I am using to search by email and it gives me NOTHING.
final DatabaseReference reference = FirebaseDatabase.instance.reference().child('chat_room');
reference.orderByChild("members").equalTo("someone@test.com").once();
Currently, this is how I save data to chat_room.
//Create chat_room
  Future<String> createChatRoom({String lastMessage, String chatMode, List<ChatMember> members}) async {
    
    var newChatRoomRef = _chatRoomReference.push();
    var newChatRoomKey = newChatRoomRef.key;
    await newChatRoomRef.set({
      'last_message': lastMessage,
      'chat_mode': chatMode,
      'members': [members[0].email, members[1].email],
      'timestamp': DateTime.now().millisecondsSinceEpoch,
      
    });
    return newChatRoomKey;
  }
Whether it is SET data structure or Array or HashMap or whatever it is, my requirement is to search the chat_rooms by email.
- If SETdata structure is the answer to this, how can I save it to firebase? How do I prepare my data so i can send it properly? Please feel free to take mycreateChatRoommethod above and show me how to passSETinstead ofArray.
- How can I perform the search?
- If setis not the answer, then what is it?
Appreciate your support.
EDIT
According to @Frank suggestion, I made my code like this, copying the exact code he provided.
//Create chat_room
  Future<String> createChatRoom(
      {String lastMessage, String chatMode, List<ChatMember> members}) async {
    var newChatRoomRef = _chatRoomReference.push();
    var newChatRoomKey = newChatRoomRef.key;
    await newChatRoomRef.set({
      'last_message': lastMessage,
      'chat_mode': chatMode,
      'members': [members[0].email, members[1].email],
      'timestamp': DateTime.now().millisecondsSinceEpoch,
    });
    var userChatRoomsRef =
        FirebaseDatabase.instance.reference().child('chat_room');
    members.forEach((member) {
      userChatRoomsRef
          .child(member.userID.toString() + "-userID")
          .child(newChatRoomKey)
          .set(true);
    });
    return newChatRoomKey;
  }
The, this is the structure it generated. (Please note that 30-userID and 50-userID are actual User IDs as I am not using UID from Firebase Auth)
Anyway I am really not sure whether this is the correct structure I should have.
EDIT 2
According to @Frank edit, this is how i MADE my code and database structure it generated.
//Create chat_room
  Future<String> createChatRoom(
      {String lastMessage, String chatMode, List<ChatMember> members}) async {
    var newChatRoomRef = _chatRoomReference.push();
    var newChatRoomKey = newChatRoomRef.key;
    await newChatRoomRef.set({
      'last_message': lastMessage,
      'chat_mode': chatMode,
      'members': [members[0].email, members[1].email],
      'timestamp': DateTime.now().millisecondsSinceEpoch,
    });
    var userChatRoomsRef =
        FirebaseDatabase.instance.reference().child('user_chatrooms');
    members.forEach((member) {
      userChatRoomsRef
          .child(member.userID.toString() + "-userID")
          .child(newChatRoomKey)
          .set(true);
    });
    return newChatRoomKey;
  }
Still now sure whether this is the right one, because I am now shifting into a different node.



 
    