deleteProfilePicture method

Future<void> deleteProfilePicture(
  1. String userId
)

Deletes a user's profile picture @param userId The ID of the user whose profile picture is to be deleted @throws Exception if the deletion fails

Implementation

Future<void> deleteProfilePicture(String userId) async {
  try {
    // Reference the file in Firebase Storage
    Reference ref = storage.child('avatars/$userId');

    // Delete the file
    await ref.delete();

    // Update the user's Firestore document to set profilePicture to an empty string
    await firestore.collection('users').doc(userId).update({'profilePicture': ''});
  } catch (e) {
    // Handle errors (e.g., file not found)
    throw Exception('Failed to delete profile picture: $e');
  }
}