🐛 Fix chat summary failed when lastMessage is null

This commit is contained in:
2025-08-22 19:00:06 +08:00
parent 701f29748d
commit b2097cf044
3 changed files with 38 additions and 30 deletions

View File

@@ -104,7 +104,7 @@ sealed class SnChatMember with _$SnChatMember {
sealed class SnChatSummary with _$SnChatSummary { sealed class SnChatSummary with _$SnChatSummary {
const factory SnChatSummary({ const factory SnChatSummary({
required int unreadCount, required int unreadCount,
required SnChatMessage lastMessage, required SnChatMessage? lastMessage,
}) = _SnChatSummary; }) = _SnChatSummary;
factory SnChatSummary.fromJson(Map<String, dynamic> json) => factory SnChatSummary.fromJson(Map<String, dynamic> json) =>

View File

@@ -213,7 +213,10 @@ Map<String, dynamic> _$SnChatMemberToJson(_SnChatMember instance) =>
_SnChatSummary _$SnChatSummaryFromJson(Map<String, dynamic> json) => _SnChatSummary _$SnChatSummaryFromJson(Map<String, dynamic> json) =>
_SnChatSummary( _SnChatSummary(
unreadCount: (json['unread_count'] as num).toInt(), unreadCount: (json['unread_count'] as num).toInt(),
lastMessage: SnChatMessage.fromJson( lastMessage:
json['last_message'] == null
? null
: SnChatMessage.fromJson(
json['last_message'] as Map<String, dynamic>, json['last_message'] as Map<String, dynamic>,
), ),
); );
@@ -221,7 +224,7 @@ _SnChatSummary _$SnChatSummaryFromJson(Map<String, dynamic> json) =>
Map<String, dynamic> _$SnChatSummaryToJson(_SnChatSummary instance) => Map<String, dynamic> _$SnChatSummaryToJson(_SnChatSummary instance) =>
<String, dynamic>{ <String, dynamic>{
'unread_count': instance.unreadCount, 'unread_count': instance.unreadCount,
'last_message': instance.lastMessage.toJson(), 'last_message': instance.lastMessage?.toJson(),
}; };
_MessageChange _$MessageChangeFromJson(Map<String, dynamic> json) => _MessageChange _$MessageChangeFromJson(Map<String, dynamic> json) =>

View File

@@ -79,18 +79,21 @@ class ChatRoomListTile extends HookConsumerWidget {
color: Theme.of(context).colorScheme.primary, color: Theme.of(context).colorScheme.primary,
), ),
), ),
if (data.lastMessage == null)
Text(room.description ?? 'descriptionNone'.tr(), maxLines: 1)
else
Row( Row(
spacing: 4, spacing: 4,
children: [ children: [
Badge( Badge(
label: Text(data.lastMessage.sender.account.nick), label: Text(data.lastMessage!.sender.account.nick),
textColor: Theme.of(context).colorScheme.onPrimary, textColor: Theme.of(context).colorScheme.onPrimary,
backgroundColor: Theme.of(context).colorScheme.primary, backgroundColor: Theme.of(context).colorScheme.primary,
), ),
Expanded( Expanded(
child: Text( child: Text(
(data.lastMessage.content?.isNotEmpty ?? false) (data.lastMessage!.content?.isNotEmpty ?? false)
? data.lastMessage.content! ? data.lastMessage!.content!
: 'messageNone'.tr(), : 'messageNone'.tr(),
maxLines: 1, maxLines: 1,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
@@ -100,7 +103,9 @@ class ChatRoomListTile extends HookConsumerWidget {
Align( Align(
alignment: Alignment.centerRight, alignment: Alignment.centerRight,
child: Text( child: Text(
RelativeTime(context).format(data.lastMessage.createdAt), RelativeTime(
context,
).format(data.lastMessage!.createdAt),
style: Theme.of(context).textTheme.bodySmall, style: Theme.of(context).textTheme.bodySmall,
), ),
), ),