Building a chat app with firebase, the functionality for a single chat between 2 users are saved. Their conversation or list of messages is being saved by a structure of merging both user_id and its peer_user_id.
The problem is in firebase merger id created are not unique, hence, shared preference is not being used.
readLocal() async {
prefs = await SharedPreferences.getInstance();
uid = prefs.getString('id') ?? '';
if (uid.hashCode <= peerId.hashCode) {
groupChatId = '$uid-$peerId';
} else {
groupChatId = '$peerId-$uid';
}
Firestore.instance.collection('users').document(uid).updateData({'chattingWith': peerId});
setState(() {});
}
Suppose user_A signs in and then with its (peer_user) user_B creating a special id with will save their conversation.
When merging their id, this happens userA_id
+ userB_id
is unique, but if user_B signs in to check messages from user_A there will be none.
As newly merged id is created with user_B+user_A, it results in a different messaging channel.
How should i merge ids that will be unique for both and will be saved their.