0

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_idis 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.

demitri kozlov
  • 247
  • 1
  • 3
  • 14
  • 1
    In this [tutorial](https://www.youtube.com/playlist?list=PLn2n4GESV0Ak1HiH0tTPTJXsOEy-5v9qb) you can find an example of a [database structure](https://www.youtube.com/watch?v=u3KwKQddPoo&list=PLn2n4GESV0Ak1HiH0tTPTJXsOEy-5v9qb&index=3) for a one-to-one chat app. So you just need to write some Flutter code. – Alex Mamo Apr 07 '20 at 13:14
  • @AlexMamo wow, it seems like a nice tutorial series. keep it up! – Angus Apr 07 '20 at 13:16
  • As said in my answer [here](https://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase), you'll need to merge the IDs lexicographically. So `var roomName = 'chat_'+(user1 – Frank van Puffelen Apr 07 '20 at 13:21

0 Answers0