I have a method to get the current user from server. When someone else logs in to the same account it return a message of Unauthenticated.
. I am throwing an exception in my method if user is unauthenticated but it does not catch it and gives error.
Future<CurrentUserUsecaseOutput> currentUser(
CurrentUserUsecaseInput input) async {
try {
final response = await _networkCallHelper.post(
EndPoints.getUser,
{},
headers: {
//
}
);
final message = response['message'];
if (message == 'Unauthenticated.') {
throw UnauthenticatedException();
} else if (message != null) {
throw MessageException(message: response['message']);
}
final data = response['user'] as Map<String, dynamic>;
final user = RestUserEntity.fromJson(data);
return CurrentUserUsecaseOutput(user: user);
} on UnauthenticatedException {
rethrow;
} on MessageException {
rethrow;
} catch (e) {
throw MessageException(message: 'Something went wrong');
}
}
on Home
Screen I am calling this on init()
What I have just missed?
Future<void> loadUser() async {
try {
await ref.read(userProvider.future);
} on UnauthenticatedException {
ref.invalidate(userProvider);
if (mounted) {
GoRouter.of(context).go(RoutePaths.login);
}
} catch (e) {
debugPrint('Error loading user: $e');
}
}