Fix: Handle getLastMessage when no message exists
parent
25b76e6274
commit
6d776874c7
|
@ -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(
|
||||||
|
'Error <last message>: ${snapshot.error}',
|
||||||
style: const TextStyle(color: Colors.red),
|
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,6 +92,14 @@ class UserTileChats extends StatelessWidget {
|
||||||
outgoing = false;
|
outgoing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return buildMsgDetails(msgContent, msgDateString, outgoing);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget buildMsgDetails(
|
||||||
|
String msgContent, String msgDateString, bool? outgoing) {
|
||||||
return Expanded(
|
return Expanded(
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
@ -133,7 +143,4 @@ class UserTileChats extends StatelessWidget {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,13 +59,16 @@ 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);
|
||||||
|
|
||||||
|
try {
|
||||||
QuerySnapshot messageSnapshot = await _firestore
|
QuerySnapshot messageSnapshot = await _firestore
|
||||||
.collection(Constants.dbCollectionChatRooms)
|
.collection(Constants.dbCollectionChatRooms)
|
||||||
.doc(chatRoomID)
|
.doc(chatRoomID)
|
||||||
|
@ -81,6 +85,10 @@ class ChatService {
|
||||||
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(
|
||||||
senderID: senderID,
|
senderID: senderID,
|
||||||
|
|
Loading…
Reference in New Issue