Compare commits

..
Author SHA1 Message Date
Thomas SharedInboxandClaude Sonnet 4.6 dee3c32a5c fix: guard threadEmails.last against empty list
Add an isEmpty check before accessing threadEmails.last to prevent
StateError when emails are deleted between query and access.

Closes #341

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-01 17:49:54 +02:00
2 changed files with 4 additions and 30 deletions
@@ -156,6 +156,7 @@ class EmailRepositoryImpl implements EmailRepository {
return;
}
if (threadEmails.isEmpty) return;
final latest = threadEmails.last;
// Collect unique participants across the whole thread.
@@ -237,12 +238,7 @@ class EmailRepositoryImpl implements EmailRepository {
try {
await client.selectMailboxByPath(emailRow.mailboxPath);
final fetch = await client.uidFetchMessage(emailRow.uid, '(BODY.PEEK[])');
final msg = fetch.messages.firstOrNull;
if (msg == null) {
throw StateError(
'IMAP server returned no message for UID ${emailRow.uid}.',
);
}
final msg = fetch.messages.first;
final textBody = msg.decodeTextPlainPart();
final rawHtml = msg.decodeTextHtmlPart();
final htmlBody =
@@ -2817,12 +2813,7 @@ class EmailRepositoryImpl implements EmailRepository {
emailRow.uid,
'BODY.PEEK[]',
);
final msg = fetch.messages.firstOrNull;
if (msg == null) {
throw StateError(
'IMAP server returned no message for UID ${emailRow.uid}.',
);
}
final msg = fetch.messages.first;
final part = msg.getPart(attachment.fetchPartId) ?? msg;
final bytes = part.decodeContentBinary();
if (bytes == null) {
@@ -2888,13 +2879,7 @@ class EmailRepositoryImpl implements EmailRepository {
emailRow.uid,
'BODY.PEEK[]',
);
final msg = fetch.messages.firstOrNull;
if (msg == null) {
throw StateError(
'IMAP server returned no message for UID ${emailRow.uid}.',
);
}
return msg.renderMessage();
return fetch.messages.first.renderMessage();
} finally {
await client.logout();
}
-11
View File
@@ -163,17 +163,6 @@ class _EmailMessageCardState extends ConsumerState<_EmailMessageCard> {
FutureBuilder<EmailBody>(
future: _bodyFuture,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Padding(
padding: const EdgeInsets.all(16),
child: Text(
'Failed to load email: ${snapshot.error}',
style: TextStyle(
color: Theme.of(context).colorScheme.error,
),
),
);
}
if (!snapshot.hasData) {
return const Center(
child: Padding(