getUserDetails method
This method retrieves the details of the currently authenticated user from Firestore. @return A Future that resolves to a UserModel object containing user details. @throws Exception if the user is not authenticated or if there is an error fetching the user details. @throws String if the user record does not exist. @throws String if there is an error fetching the user details.
Implementation
Future<UserModel> getUserDetails() async {
try {
final auth = FirebaseAuth.instance;
final userId = auth.currentUser?.uid;
if (userId == null) {
throw Exception('User is not authenticated');
}
final snapshot = await _db.collection("users").doc(userId).get();
if (!snapshot.exists) throw 'No such user found';
return UserModel.fromSnapshot(snapshot);
} catch (e) {
throw e
.toString()
.isEmpty ? 'Something went wrong. Please Try Again' : e.toString();
}
}