diff --git a/lib/components/user_tile_chats.dart b/lib/components/user_tile_chats.dart index 5e21bfc..0a53e99 100644 --- a/lib/components/user_tile_chats.dart +++ b/lib/components/user_tile_chats.dart @@ -72,12 +72,14 @@ class UserTileChats extends StatelessWidget { if (snapshot.connectionState == ConnectionState.waiting) { return const CircularProgressIndicator(); } else if (snapshot.hasError) { - return Text( - 'Error: ${snapshot.error}', - style: const TextStyle(color: Colors.red), + return Expanded( + child: Text( + 'Error : ${snapshot.error}', + style: const TextStyle(color: Colors.red), + ), ); } else if (!snapshot.hasData || snapshot.data == null) { - return const Text('No messages yet'); + return buildMsgDetails('No messages yet', '', null); } else { Message lastMessage = snapshot.data!; msgDateString = formatTimestamp(lastMessage.timestamp); @@ -90,50 +92,55 @@ class UserTileChats extends StatelessWidget { outgoing = false; } - return Expanded( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisAlignment: MainAxisAlignment.start, - children: [ - // user name - Text( - headerText, - style: const TextStyle( - fontSize: 20, - fontWeight: FontWeight.w500, - ), - ), - const SizedBox(height: 4), - Text(msgContent, overflow: TextOverflow.ellipsis, maxLines: 1), - Row( - children: [ - if (outgoing == true) - const Icon( - Icons.call_made, - color: Colors.green, - size: 16, - ), - if (outgoing == false) - const Icon( - Icons.call_received, - color: Colors.blue, - size: 16, - ), - const SizedBox(width: 4), - Flexible( - child: Text( - msgDateString, - overflow: TextOverflow.ellipsis, - maxLines: 1, - ), - ), - ], - ), - ], - ), - ); + return buildMsgDetails(msgContent, msgDateString, outgoing); } }, ); } + + Widget buildMsgDetails( + String msgContent, String msgDateString, bool? outgoing) { + return Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisAlignment: MainAxisAlignment.start, + children: [ + // user name + Text( + headerText, + style: const TextStyle( + fontSize: 20, + fontWeight: FontWeight.w500, + ), + ), + const SizedBox(height: 4), + Text(msgContent, overflow: TextOverflow.ellipsis, maxLines: 1), + Row( + children: [ + if (outgoing == true) + const Icon( + Icons.call_made, + color: Colors.green, + size: 16, + ), + if (outgoing == false) + const Icon( + Icons.call_received, + color: Colors.blue, + size: 16, + ), + const SizedBox(width: 4), + Flexible( + child: Text( + msgDateString, + overflow: TextOverflow.ellipsis, + maxLines: 1, + ), + ), + ], + ), + ], + ), + ); + } } diff --git a/lib/services/chat/chat_service.dart b/lib/services/chat/chat_service.dart index e40c733..f896074 100644 --- a/lib/services/chat/chat_service.dart +++ b/lib/services/chat/chat_service.dart @@ -1,5 +1,6 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; +import 'package:flutter/foundation.dart' show debugPrint; import '../../constants.dart'; import '../../utils/helper.dart'; @@ -58,28 +59,35 @@ class ChatService { } /// Retrieves the last message of a chatroom. - Future getLastMessage(String chatRoomID) async { + /// + /// Returns [null] in case of an error. + Future getLastMessage(String chatRoomID) async { String senderID = ''; String senderEmail = ''; String receiverID = ''; String message = ''; - Timestamp timestamp = Timestamp.fromDate(DateTime.utc(1970, 01, 01)); + Timestamp timestamp = Timestamp.fromMillisecondsSinceEpoch(0); - QuerySnapshot messageSnapshot = await _firestore - .collection(Constants.dbCollectionChatRooms) - .doc(chatRoomID) - .collection(Constants.dbCollectionMessages) - .orderBy(Constants.dbFieldMessageTimestamp, descending: true) - .limit(1) - .get(); + try { + QuerySnapshot messageSnapshot = await _firestore + .collection(Constants.dbCollectionChatRooms) + .doc(chatRoomID) + .collection(Constants.dbCollectionMessages) + .orderBy(Constants.dbFieldMessageTimestamp, descending: true) + .limit(1) + .get(); - DocumentSnapshot lastMessageDoc = messageSnapshot.docs.first; - if (lastMessageDoc.exists) { - senderID = lastMessageDoc[Constants.dbFieldMessageSenderId]; - senderEmail = lastMessageDoc[Constants.dbFieldMessageSenderEmail]; - receiverID = lastMessageDoc[Constants.dbFieldMessageReceiverId]; - message = lastMessageDoc[Constants.dbFieldMessageText]; - timestamp = lastMessageDoc[Constants.dbFieldMessageTimestamp]; + DocumentSnapshot lastMessageDoc = messageSnapshot.docs.first; + if (lastMessageDoc.exists) { + senderID = lastMessageDoc[Constants.dbFieldMessageSenderId]; + senderEmail = lastMessageDoc[Constants.dbFieldMessageSenderEmail]; + receiverID = lastMessageDoc[Constants.dbFieldMessageReceiverId]; + message = lastMessageDoc[Constants.dbFieldMessageText]; + timestamp = lastMessageDoc[Constants.dbFieldMessageTimestamp]; + } + } catch (e) { + debugPrint(e.toString()); + return null; } return Message(