flutter-chat-demo
flutter-chat-demo copied to clipboard
Get users messages
Is it possible from this data model to get a list of my previous chats with last message shown. Like the one in Messenger or WhatsApp after you login...
Right now you save the document in collection like idFrom - idTo ... But I am struggling to query it for a user with uid.
Sure! At the main.dart add this widget inside buildItem.
Widget getLastMessage(String userId, String peerId) {
String groupChatId = '';
if (userId.hashCode <= peerId.hashCode) {
groupChatId = '$userId-$peerId';
} else {
groupChatId = '$peerId-$userId';
}
return StreamBuilder(
stream: _firestore
.collection('messages')
.document(groupChatId)
.collection(groupChatId)
.orderBy('timestamp', descending: true)
.limit(1)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text('Loading...');
} else {
var documentsData = snapshot.data.documents;
if (documentsData[0]['type'] == 1) {
return Text('Image message');
} else {
return Text(documentsData[0]['content']);
}
}
}
);
}
But if I don't know apeerId
?
I contacted person A, B and C. I need an overview of last chats, something like this
https://i.stack.imgur.com/OliD7.jpg
So basically you are mapping over the list of users you have chatted. So you have avatar url, user name & id. In this case while mapping items call the Widget getLastMessage with the peerId
of current item id.
Hopefully I was clear! :)
If I understood correctly, then in this case I have to save that list of users I've chatted with (in DB, or cache)?
Yes!
If you are coming from this project example as I said you should call the Widget getLastMessage
at the main.dart
inside buildItem
Whenever a chat is initiated, please check if its the first chat between the current user and the peer, if its the case, save a new instance of that chat with time reference in a firestore table called chats, also save the last message there. when the app loads, take a chat list from that table.in this case, the last message will be stored there. if this is not the first chat, just update the time and the last message sent. this was my approach
Yes! If you are coming from this project example as I said you should call the
Widget getLastMessage
at themain.dart
insidebuildItem
i added the code snippet to my code and this is the error i got
instead of the actual message this is what i got
@akiligedeon I referred your idea and did a similar thing, but one thing I am not getting is that when the user list will be retrieved with the last message, if the last message is a image, how should it be dealt with?
Hi,
Good one. I am using this project example. If current user or peer user get new message, it is coming as push notification in notification tray. Along with push notification, i want to show new message count in chat list screen as badge like whatsapp also.
Attached sample image. How can i acheive this. Please help me on this.
You can achieve using combine latest method in rx dart, by having the mesage for each user in different document and listening to it
On 18 Aug 2020 7:08 am, "MaheshPeri19" [email protected] wrote:
Hi,
Good one. I am using this project example. If current user or peer user get new message, it is coming as push notification in notification tray. Just i want to show new message count in chat list screen as badge like whatsapp.
Attached sample image. How can i acheive this. Please help me on this.
[image: whatsapp_badgecount] https://user-images.githubusercontent.com/58836654/90476448-294c1100-e147-11ea-9c1b-2d8817eb0a71.PNG
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/duytq94/flutter-chat-demo/issues/34#issuecomment-675274380, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJYIBIQTINDYKKT5TZ6VVVTSBILGFANCNFSM4KJBX53A .
Ok. Thanks for your reply. Any other suggestion without using rx dart package ? As i am not aware of Rxdart.
https://m.youtube.com/watch?v=B4ZFVBm-Ugs&t=2s https://m.youtube.com/watch?v=-7EaYUIRBmc&t=6s
watch these two videos ,one deal with combine latest the othere deals with stream
Thank you. i will watch.
Thank you
Le mar. 18 août 2020 à 18:22, MaheshPeri19 [email protected] a écrit :
Thank you. i will watch.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/duytq94/flutter-chat-demo/issues/34#issuecomment-675578637, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALI3IZBARRE3EXBGEPH5YELSBKTDNANCNFSM4KJBX53A .
@Baksman i am trying to achieve something like that whatsapp like the image the guy share above but i have watch the video and i dont no how to go about it