fix: Add mounted checks before state updates to prevent errors on disposed providers.
This commit is contained in:
@@ -388,6 +388,7 @@ class AgentDetailNotifier extends StateNotifier<AgentDetailState> {
|
||||
_repo.getFieldValues(agentId),
|
||||
_repo.getTestimonials(agentId),
|
||||
]);
|
||||
if (!mounted) return;
|
||||
state = state.copyWith(
|
||||
agent: results[0] as AgentProfile,
|
||||
fieldValues: results[1] as List<AgentFieldValue>,
|
||||
@@ -395,6 +396,7 @@ class AgentDetailNotifier extends StateNotifier<AgentDetailState> {
|
||||
isLoading: false,
|
||||
);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
state = state.copyWith(isLoading: false, error: e.toString());
|
||||
}
|
||||
}
|
||||
@@ -406,6 +408,7 @@ class AgentDetailNotifier extends StateNotifier<AgentDetailState> {
|
||||
_repo.getFieldValues(agentId),
|
||||
_repo.getTestimonials(agentId),
|
||||
]);
|
||||
if (!mounted) return;
|
||||
state = state.copyWith(
|
||||
agent: results[0] as AgentProfile,
|
||||
fieldValues: results[1] as List<AgentFieldValue>,
|
||||
@@ -419,12 +422,14 @@ class AgentDetailNotifier extends StateNotifier<AgentDetailState> {
|
||||
|
||||
Future<void> loadConnectionStatus() async {
|
||||
final status = await _repo.getConnectionStatus(agentId);
|
||||
if (!mounted) return;
|
||||
state = state.copyWith(connectionStatus: status);
|
||||
}
|
||||
|
||||
Future<bool> connect({String? message}) async {
|
||||
final success =
|
||||
await _repo.createConnectionRequest(agentId, message: message);
|
||||
if (!mounted) return success;
|
||||
if (success) {
|
||||
state = state.copyWith(
|
||||
connectionStatus: {'status': 'PENDING'},
|
||||
@@ -444,6 +449,7 @@ class AgentDetailNotifier extends StateNotifier<AgentDetailState> {
|
||||
final requestId = state.connectionRequestId;
|
||||
if (requestId == null) return false;
|
||||
final success = await _repo.cancelConnectionRequest(requestId);
|
||||
if (!mounted) return success;
|
||||
if (success) {
|
||||
state = state.copyWith(clearConnection: true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user