fix(repo): guard moveEmail/setFlag/deleteEmail against missing rows

getSingle() throws 'Bad state: No element' when the email row is gone
(race condition in batch operations or already deleted). Switch to
getSingleOrNull() and return early so batch moves/flags/deletes on
stale IDs fail silently instead of crashing.

Closes #58

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Thomas SharedInbox
2026-05-14 20:32:25 +02:00
co-authored by Claude Sonnet 4.6
parent ca28bd01af
commit 2985198d9c
@@ -1448,7 +1448,8 @@ class EmailRepositoryImpl implements EmailRepository {
final row = await (_db.select(
_db.emails,
)..where((t) => t.id.equals(emailId)))
.getSingle();
.getSingleOrNull();
if (row == null) return;
final account = (await _accounts.getAccount(row.accountId))!;
if (account.type == account_model.AccountType.jmap) {
@@ -1582,7 +1583,8 @@ class EmailRepositoryImpl implements EmailRepository {
final row = await (_db.select(
_db.emails,
)..where((t) => t.id.equals(emailId)))
.getSingle();
.getSingleOrNull();
if (row == null) return;
final account = (await _accounts.getAccount(row.accountId))!;
if (row.mailboxPath == destMailboxPath) {
@@ -1650,7 +1652,8 @@ class EmailRepositoryImpl implements EmailRepository {
final row = await (_db.select(
_db.emails,
)..where((t) => t.id.equals(emailId)))
.getSingle();
.getSingleOrNull();
if (row == null) return null;
final account = (await _accounts.getAccount(row.accountId))!;
// Move to Trash when possible so the user can recover the message.