924
bool streamHadError = false;
925
926
void updateUiState() {
927
- if (!stateNeedsUpdate) return;
927
+ if (!stateNeedsUpdate || !ref.mounted) return;
928
stateNeedsUpdate = false;
929
930
final streamConvId = assistantMessage.conversationId;
951
)
952
.listen(
953
(response) async {
954
- if (streamGeneration != _streamGeneration) return;
954
+ if (streamGeneration != _streamGeneration || !ref.mounted) {
955
+ return;
956
+ }
957
final streamConvId = assistantMessage.conversationId;
958
final isCurrentContext = _currentConversationId == streamConvId;
959
1072
if (!_isInMemoryChat) {
1073
await _saveService?.flush();
1074
}
1073
- ref.read(chatBackgroundServiceProvider).stop();
1075
+ if (ref.mounted) {
1076
+ ref.read(chatBackgroundServiceProvider).stop();
1077
+ }
1078
return;
1079
}
1080
1081
if (!_isInMemoryChat) {
1082
await _saveService?.flush();
1083
}
1080
- ref.read(chatBackgroundServiceProvider).stop();
1084
+ if (ref.mounted) {
1085
+ ref.read(chatBackgroundServiceProvider).stop();
1086
+ }
1087
final streamConvId = assistantMessage.conversationId;
1088
final isCurrentContext = _activeConversationId == streamConvId;
1089
final streamingMessage = streamingAssistantMessage;
1100
isProcessing: false,
1101
);
1102
await _saveMessage(errorMessage);
1097
- if (isCurrentContext) {
1103
+ if (ref.mounted && isCurrentContext) {
1104
_replaceMessageInState(errorMessage, clearStreaming: true);
1105
state = state.copyWith(
1106
isStreaming: false,
1107
errorMessage: errorMessage.errorMessage,
1108
);
1109
}
1104
- ref.read(isStreamingProvider.notifier).setStreaming(false);
1110
+ if (ref.mounted) {
1111
+ ref.read(isStreamingProvider.notifier).setStreaming(false);
1112
+ }
1113
_latestStreamingMessage = null;
1114
} else {
1115
var finalMessage = _finalizeStreamMessage(
1164
}
1165
}
1166
1159
- if (clientExecuted.isNotEmpty) {
1167
+ if (clientExecuted.isNotEmpty && ref.mounted) {
1168
final registry = ref.read(toolRegistryProvider);
1169
final adapter = createAdapterForServerType(server.type);
1170
1188
completer: completer,
1189
);
1190
_pendingToolApproval = approval;
1183
- state = state.copyWith(
1184
- pendingToolApproval: approval,
1185
- );
1191
+ if (ref.mounted) {
1192
+ state = state.copyWith(
1193
+ pendingToolApproval: approval,
1194
+ );
1195
+ }
1196
1197
final result = await completer.future;
1198
1199
_pendingToolApproval = null;
1190
- state = state.copyWith(clearPendingApproval: true);
1200
+ if (ref.mounted) {
1201
+ state = state.copyWith(
1202
+ clearPendingApproval: true,
1203
+ );
1204
+ }
1205
1206
return result;
1207
},
1229
}
1230
1231
await _saveMessage(finalMessage);
1218
- if (isCurrentContext) {
1232
+ if (ref.mounted && isCurrentContext) {
1233
_replaceMessageInState(finalMessage, clearStreaming: true);
1234
state = state.copyWith(isStreaming: false);
1235
_maybeAutoGenerateTitleAfterFirstReply();
1236
}
1223
- ref.read(isStreamingProvider.notifier).setStreaming(false);
1237
+ if (ref.mounted) {
1238
+ ref.read(isStreamingProvider.notifier).setStreaming(false);
1239
+ }
1240
_latestStreamingMessage = null;
1241
1242
await _syncConversationStatsAfterGeneration(
1245
isCurrentContext: isCurrentContext,
1246
);
1247
1232
- _maybeRequestReviewAfterSuccessfulCompletion(
1233
- finalMessage: finalMessage,
1234
- server: server,
1235
- selectedModel: selectedModel,
1236
- );
1248
+ if (ref.mounted) {
1249
+ _maybeRequestReviewAfterSuccessfulCompletion(
1250
+ finalMessage: finalMessage,
1251
+ server: server,
1252
+ selectedModel: selectedModel,
1253
+ );
1254
+ }
1255
1256
_chunkCount = 0;
1257
_lastCheckpointTime = null;
1260
1261
// Auto-speak: read the response aloud if the setting
1262
// is enabled and voice mode is not already handling TTS.
1245
- final autoSpeak = ref.read(settingsProvider).autoSpeakEnabled;
1246
- final voiceActive =
1247
- ref.read(voiceModeProvider).phase != VoiceModePhase.idle;
1248
- if (autoSpeak &&
1249
- !voiceActive &&
1250
- finalMessage.content.trim().isNotEmpty) {
1251
- ref
1252
- .read(ttsProvider.notifier)
1253
- .speak(
1254
- finalMessage.content,
1255
- messageId: finalMessage.id,
1256
- conversationId: finalMessage.conversationId,
1257
- );
1263
+ if (ref.mounted) {
1264
+ final autoSpeak = ref
1265
+ .read(settingsProvider)
1266
+ .autoSpeakEnabled;
1267
+ final voiceActive =
1268
+ ref.read(voiceModeProvider).phase !=
1269
+ VoiceModePhase.idle;
1270
+ if (autoSpeak &&
1271
+ !voiceActive &&
1272
+ finalMessage.content.trim().isNotEmpty) {
1273
+ ref
1274
+ .read(ttsProvider.notifier)
1275
+ .speak(
1276
+ finalMessage.content,
1277
+ messageId: finalMessage.id,
1278
+ conversationId: finalMessage.conversationId,
1279
+ );
1280
+ }
1281
}
1282
}
1283
},
1298
if (errorMessage != null) {
1299
await _saveMessage(errorMessage);
1300
1278
- ref.read(chatBackgroundServiceProvider).stop();
1301
+ if (ref.mounted) {
1302
+ ref.read(chatBackgroundServiceProvider).stop();
1303
+ }
1304
1305
final streamConvId = assistantMessage.conversationId;
1281
- if (_currentConversationId == streamConvId) {
1306
+ if (ref.mounted && _currentConversationId == streamConvId) {
1307
_replaceMessageInState(errorMessage, clearStreaming: true);
1308
state = state.copyWith(
1309
isStreaming: false,
1311
);
1312
}
1313
}
1289
- ref.read(isStreamingProvider.notifier).setStreaming(false);
1314
+ if (ref.mounted) {
1315
+ ref.read(isStreamingProvider.notifier).setStreaming(false);
1316
+ }
1317
_latestStreamingMessage = null;
1318
},
1319
);
1328
);
1329
await _saveMessage(errorMsg);
1330
1331
+ if (!ref.mounted) return;
1332
+
1333
state = state.copyWith(
1334
isStreaming: false,
1335
errorMessage: e.toString(),
1419
1420
await _saveMessage(message);
1421
1422
+ if (!ref.mounted) return;
1423
+
1424
if (!_isInMemoryChat && _currentConversationId != null) {
1425
final preview = trimmedContent.isNotEmpty
1426
? (trimmedContent.length > 100
2215
);
2216
_maybeAutoGenerateTitleAfterFirstReply();
2217
}
2187
- ref.read(isStreamingProvider.notifier).setStreaming(false);
2188
- ref.read(chatBackgroundServiceProvider).stop();
2218
+ if (ref.mounted) {
2219
+ ref.read(isStreamingProvider.notifier).setStreaming(false);
2220
+ ref.read(chatBackgroundServiceProvider).stop();
2221
+ }
2222
await _syncConversationStatsAfterGeneration(
2223
streamConvId,
2224
finalMessage,
2233
2234
// Auto-speak: read the response aloud if the setting is
2235
// enabled and voice mode is not already handling TTS.
2203
- final autoSpeak = ref.read(settingsProvider).autoSpeakEnabled;
2204
- final voiceActive =
2205
- ref.read(voiceModeProvider).phase != VoiceModePhase.idle;
2206
- if (autoSpeak &&
2207
- !voiceActive &&
2208
- finalMessage.content.trim().isNotEmpty) {
2209
- ref
2210
- .read(ttsProvider.notifier)
2211
- .speak(
2212
- finalMessage.content,
2213
- messageId: finalMessage.id,
2214
- conversationId: finalMessage.conversationId,
2215
- );
2236
+ if (ref.mounted) {
2237
+ final autoSpeak = ref.read(settingsProvider).autoSpeakEnabled;
2238
+ final voiceActive =
2239
+ ref.read(voiceModeProvider).phase != VoiceModePhase.idle;
2240
+ if (autoSpeak &&
2241
+ !voiceActive &&
2242
+ finalMessage.content.trim().isNotEmpty) {
2243
+ ref
2244
+ .read(ttsProvider.notifier)
2245
+ .speak(
2246
+ finalMessage.content,
2247
+ messageId: finalMessage.id,
2248
+ conversationId: finalMessage.conversationId,
2249
+ );
2250
+ }
2251
}
2252
},
2253
onError: (Object error, StackTrace stackTrace) async {
2271
errorMessage: error.toString(),
2272
);
2273
}
2239
- ref.read(isStreamingProvider.notifier).setStreaming(false);
2240
- ref.read(chatBackgroundServiceProvider).stop();
2274
+ if (ref.mounted) {
2275
+ ref.read(isStreamingProvider.notifier).setStreaming(false);
2276
+ ref.read(chatBackgroundServiceProvider).stop();
2277
+ }
2278
},
2279
);
2280
} catch (e) {
2567
}
2568
2569
Future<void> _saveMessage(Message message) async {
2533
- if (_isInMemoryChat) return;
2570
+ if (_isInMemoryChat || !ref.mounted) return;
2571
final db = ref.read(databaseProvider);
2572
await db.store.runInTransactionAsync(
2573
TxMode.write,
2607
2608
Future<void> clearConversation() async {
2609
await _saveService?.flush();
2610
+ if (!ref.mounted) return;
2611
final db = ref.read(databaseProvider);
2612
if (_currentConversationId != null && !_isInMemoryChat) {
2613
final query = db.messageBox