生成一个带有两个userid的chatRoomId


Future<Stream<QuerySnapshot>> getChatMessages() async {
return chatRoomsRef
.doc(getChatRoomIdByUserId(widget.chatter1.id, widget.chatter2.id))
.collection("messages")
.orderBy("TimeStamp", descending: true)
.snapshots();
}  
getChatRoomIdByUserId(String a, String b) {
if (a.substring(0, 1).codeUnitAt(0) > b.substring(0, 1).codeUnitAt(0)) {
return "$b-$a";
} else {
return "$a-$b";
}
}

问题是我想创建一个名为chatRoomId的字符串,如果您从用户id b或用户id a输入聊天无关紧要…它应该读写相同的chatRoomId。

但当我尝试它创建两个chatroomid…

106660068626993723901 - 106660068626993723901110397997856010677626 - 110397997856010677626

可以试试这个函数吗?

String getThreadIdFromFriendId(String friendId, String currentUid) {
List<String> uids = [friendId, currentUid];
uids.sort((uid1, uid2) => uid1.compareTo(uid2));
return uids.join("_");
}
String getChatRoomIdByUserId(String userID, String peerID) {
return userID.hashCode <= peerID.hashCode
? userID + '_' + peerID
: peerID + '_' + userID;
}

相关内容

  • 没有找到相关文章