Fix: Handle getLastMessage when no message exists

master
Rafael 2024-06-06 18:45:23 +02:00
parent 25b76e6274
commit 6d776874c7
2 changed files with 77 additions and 62 deletions

View File

@ -72,12 +72,14 @@ class UserTileChats extends StatelessWidget {
if (snapshot.connectionState == ConnectionState.waiting) { if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator(); return const CircularProgressIndicator();
} else if (snapshot.hasError) { } else if (snapshot.hasError) {
return Text( return Expanded(
'Error: ${snapshot.error}', child: Text(
style: const TextStyle(color: Colors.red), 'Error <last message>: ${snapshot.error}',
style: const TextStyle(color: Colors.red),
),
); );
} else if (!snapshot.hasData || snapshot.data == null) { } else if (!snapshot.hasData || snapshot.data == null) {
return const Text('No messages yet'); return buildMsgDetails('No messages yet', '', null);
} else { } else {
Message lastMessage = snapshot.data!; Message lastMessage = snapshot.data!;
msgDateString = formatTimestamp(lastMessage.timestamp); msgDateString = formatTimestamp(lastMessage.timestamp);
@ -90,50 +92,55 @@ class UserTileChats extends StatelessWidget {
outgoing = false; outgoing = false;
} }
return Expanded( return buildMsgDetails(msgContent, msgDateString, outgoing);
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,
),
),
],
),
],
),
);
} }
}, },
); );
} }
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,
),
),
],
),
],
),
);
}
} }

View File

@ -1,5 +1,6 @@
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart' show debugPrint;
import '../../constants.dart'; import '../../constants.dart';
import '../../utils/helper.dart'; import '../../utils/helper.dart';
@ -58,28 +59,35 @@ class ChatService {
} }
/// Retrieves the last message of a chatroom. /// Retrieves the last message of a chatroom.
Future<Message> getLastMessage(String chatRoomID) async { ///
/// Returns [null] in case of an error.
Future<Message?> getLastMessage(String chatRoomID) async {
String senderID = ''; String senderID = '';
String senderEmail = ''; String senderEmail = '';
String receiverID = ''; String receiverID = '';
String message = ''; String message = '';
Timestamp timestamp = Timestamp.fromDate(DateTime.utc(1970, 01, 01)); Timestamp timestamp = Timestamp.fromMillisecondsSinceEpoch(0);
QuerySnapshot messageSnapshot = await _firestore try {
.collection(Constants.dbCollectionChatRooms) QuerySnapshot messageSnapshot = await _firestore
.doc(chatRoomID) .collection(Constants.dbCollectionChatRooms)
.collection(Constants.dbCollectionMessages) .doc(chatRoomID)
.orderBy(Constants.dbFieldMessageTimestamp, descending: true) .collection(Constants.dbCollectionMessages)
.limit(1) .orderBy(Constants.dbFieldMessageTimestamp, descending: true)
.get(); .limit(1)
.get();
DocumentSnapshot lastMessageDoc = messageSnapshot.docs.first; DocumentSnapshot lastMessageDoc = messageSnapshot.docs.first;
if (lastMessageDoc.exists) { if (lastMessageDoc.exists) {
senderID = lastMessageDoc[Constants.dbFieldMessageSenderId]; senderID = lastMessageDoc[Constants.dbFieldMessageSenderId];
senderEmail = lastMessageDoc[Constants.dbFieldMessageSenderEmail]; senderEmail = lastMessageDoc[Constants.dbFieldMessageSenderEmail];
receiverID = lastMessageDoc[Constants.dbFieldMessageReceiverId]; receiverID = lastMessageDoc[Constants.dbFieldMessageReceiverId];
message = lastMessageDoc[Constants.dbFieldMessageText]; message = lastMessageDoc[Constants.dbFieldMessageText];
timestamp = lastMessageDoc[Constants.dbFieldMessageTimestamp]; timestamp = lastMessageDoc[Constants.dbFieldMessageTimestamp];
}
} catch (e) {
debugPrint(e.toString());
return null;
} }
return Message( return Message(