fix: Add mounted checks before state updates to prevent errors on disposed providers.

This commit is contained in:
pradeepkumar
2026-03-15 17:07:18 +05:30
parent 8884038842
commit 1fafbb1eb4

View File

@@ -388,6 +388,7 @@ class AgentDetailNotifier extends StateNotifier<AgentDetailState> {
_repo.getFieldValues(agentId), _repo.getFieldValues(agentId),
_repo.getTestimonials(agentId), _repo.getTestimonials(agentId),
]); ]);
if (!mounted) return;
state = state.copyWith( state = state.copyWith(
agent: results[0] as AgentProfile, agent: results[0] as AgentProfile,
fieldValues: results[1] as List<AgentFieldValue>, fieldValues: results[1] as List<AgentFieldValue>,
@@ -395,6 +396,7 @@ class AgentDetailNotifier extends StateNotifier<AgentDetailState> {
isLoading: false, isLoading: false,
); );
} catch (e) { } catch (e) {
if (!mounted) return;
state = state.copyWith(isLoading: false, error: e.toString()); state = state.copyWith(isLoading: false, error: e.toString());
} }
} }
@@ -406,6 +408,7 @@ class AgentDetailNotifier extends StateNotifier<AgentDetailState> {
_repo.getFieldValues(agentId), _repo.getFieldValues(agentId),
_repo.getTestimonials(agentId), _repo.getTestimonials(agentId),
]); ]);
if (!mounted) return;
state = state.copyWith( state = state.copyWith(
agent: results[0] as AgentProfile, agent: results[0] as AgentProfile,
fieldValues: results[1] as List<AgentFieldValue>, fieldValues: results[1] as List<AgentFieldValue>,
@@ -419,12 +422,14 @@ class AgentDetailNotifier extends StateNotifier<AgentDetailState> {
Future<void> loadConnectionStatus() async { Future<void> loadConnectionStatus() async {
final status = await _repo.getConnectionStatus(agentId); final status = await _repo.getConnectionStatus(agentId);
if (!mounted) return;
state = state.copyWith(connectionStatus: status); state = state.copyWith(connectionStatus: status);
} }
Future<bool> connect({String? message}) async { Future<bool> connect({String? message}) async {
final success = final success =
await _repo.createConnectionRequest(agentId, message: message); await _repo.createConnectionRequest(agentId, message: message);
if (!mounted) return success;
if (success) { if (success) {
state = state.copyWith( state = state.copyWith(
connectionStatus: {'status': 'PENDING'}, connectionStatus: {'status': 'PENDING'},
@@ -444,6 +449,7 @@ class AgentDetailNotifier extends StateNotifier<AgentDetailState> {
final requestId = state.connectionRequestId; final requestId = state.connectionRequestId;
if (requestId == null) return false; if (requestId == null) return false;
final success = await _repo.cancelConnectionRequest(requestId); final success = await _repo.cancelConnectionRequest(requestId);
if (!mounted) return success;
if (success) { if (success) {
state = state.copyWith(clearConnection: true); state = state.copyWith(clearConnection: true);
} }