I am developing a Chat application with Flutter and Firebase Realtime Database. Below is my JSON Stucture.
{
"chat_room" : {
"-MMR-dPo0A0nG4hcVW2j" : {
"chat_mode" : "Supplier",
"last_message" : "Test",
"timestamp" : 1605713958580
},
"-MMRMbKC-818t2rg9UU5" : {
"chat_mode" : "Supplier",
"last_message" : "Test",
"timestamp" : 1605719979342
}
},
"members" : {
"-MMR-dPo0A0nG4hcVW2j" : {
"-MMR-dUUrMP1pDUZDMU3" : {
"email" : "someone@test.com",
"profile_pic" : "",
"userID" : 30,
"user_role" : "customer"
},
"-MMR-dYyDNdZfyLdCHD1" : {
"email" : "anotherone@test.com",
"userID" : 50,
"user_role" : "supplier"
}
},
"-MMRMbKC-818t2rg9UU5" : {
"-MMRMbP_ucK89HU-yKaW" : {
"email" : "someone@test.com",
"profile_pic" : "",
"userID" : 30,
"user_role" : "customer"
},
"-MMRMbTzL3x3I9BqLxOD" : {
"email" : "anotherone@test.com",
"userID" : 50,
"user_role" : "supplier"
}
}
}
}
- I am first creating a
chat_room
- Then I am adding the chat room users into the
members
- To identify which
member
belong to whichchat_room
, I am adding thechat_room
unique key as theforeign key
inmembers
and adding all the relatedmembers
under that.
Now my question is, I am in need of searching the members
by the email
attribute and get all member
records associated with. For an example, I need to search members
for the records where the email
is someone@test.com
.
This is my current code, but its not giving what I need. I also know it is incorrect.
final DatabaseReference reference = FirebaseDatabase.instance.reference().child('members');
reference.orderByChild("email").equalTo("someone@test.com").once()
How can I perform this data retrieve in Flutter? If my chat app data structure is invalid, I am open for suggestions for a better structure.