getUserDetailsById method

Future<UserModel> getUserDetailsById(
  1. String userId
)

This method retrieves user details by their ID. @param userId The ID of the user whose details are to be retrieved. @return A Future that resolves to a UserModel object containing 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> getUserDetailsById(String userId) async {
  try {
    final snapshot = await _db.collection("users").doc(userId).get();
    if (!snapshot.exists) {
      throw Exception('No user found with the given ID');
    }
    return UserModel.fromSnapshot(snapshot);
  } on FirebaseException catch (e) {
    throw e.message.toString();
  } catch (e) {
    throw 'Something went wrong. Please try again. Details: $e';
  }
}