This is how my Firestore look like. I am trying to query the 'chatIds' collection to get the 'chatId' containing the two users
final userIds = [
'${firebaseAuth.currentUser!.uid}',
'${user.id}',
];
StreamBuilder(
stream: chatIdRef
.where("users", isEqualTo: userIds)
.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
var snap = snapshot.data;
List docs = snap!.docs;
print(docs.length.toString());
print(snapshot.data!.docs.toString());
return docs.isEmpty
? IconButton(
onPressed: () {
widget.isMe
? Navigate.pushPage(
context, EditProfile(user: user))
: Navigate.pushPage(
context,
Conversation(
userId: user.id!,
chatId: 'newChat',
),
);
},
icon: widget.isMe
? Icon(Ionicons.create_outline)
: Icon(Ionicons.chatbox_ellipses_outline),
)
: IconButton(
onPressed: () {
QueryDocumentSnapshot doc = docs[0];
widget.isMe
? Navigate.pushPage(
context, EditProfile(user: user))
: Navigate.pushPage(
context,
Conversation(
userId: user.id!,
chatId:
doc.get('chatId').toString(),
),
);
},
icon: widget.isMe
? Icon(Ionicons.create_outline)
: Icon(Ionicons.chatbox_ellipses_outline),
);
} else {
return IconButton(
onPressed: () {
widget.isMe
? Navigate.pushPage(
context, EditProfile(user: user))
: Navigate.pushPage(
context,
Conversation(
userId: user.id!,
chatId: 'newChat',
),
);
},
icon: widget.isMe
? Icon(Ionicons.create_outline)
: Icon(Ionicons.chatbox_ellipses_outline),
);
}
},
),
The code above works partially, sometimes it doesn't return the chatId until I swap the usersIds i.e instead of doing this:
final userIds = [
'${firebaseAuth.currentUser!.uid}',
'${user.id}',
];
I do this:
final userIds = [
'${user.id}',
'${firebaseAuth.currentUser!.uid}',
];
My question is this, Is there a way I can query the chatIds collection to get the documents containing the 2 userIds I supplied?