fetchUserData method
Fetches the latest user data from the API.
This method is useful for refreshing the user's profile information.
It updates the internal user object and persists it to shared_preferences
.
Returns true
if data is fetched successfully, false
otherwise.
Implementation
Future<bool> fetchUserData() async {
_setLoading(true);
_clearError();
try {
if (_user == null || _hospital == null) {
throw Exception('User or hospital data not available');
}
final response = await _apiService.getUser(_hospital!.id, _user!.id);
final updatedUser = User.fromJson(response);
_user = updatedUser;
final prefs = await SharedPreferences.getInstance();
await prefs.setString('user', jsonEncode(updatedUser.toJson()));
notifyListeners();
return true;
} catch (e) {
_setError(parseApiError(e.toString()));
return false;
} finally {
_setLoading(false);
}
}