fetchDocumentById method

Future<Map<String, dynamic>?> fetchDocumentById({
  1. required String docId,
})

Fetches a document from Firestore by its ID.

This method logs the progress, performs the fetch operation, and handles errors. The logger also records the completion time.

docId: The ID of the document to fetch.

Returns a Future of DocumentSnapshot containing the document data.

Implementation

Future<Map<String, dynamic>?> fetchDocumentById(
    {required String docId}) async {
  // Log the start of the fetch operation
  final now = DateTime.now();
  loggerService?.log("⌛ Fetching document with ID $docId in progress");

  try {
    // Fetch document from Firestore
    final result =
        await firestoreReadService.fetchDocumentById(collection, docId);
    return result.data() as Map<String, dynamic>?;
  } catch (e) {
    // Log any error encountered during the fetch
    final errorMessage = 'Error fetching document with ID $docId';
    loggerService?.logError(errorMessage, e.toString());
    // Rethrow the error for further handling
    rethrow;
  } finally {
    // Log the completion time of the fetch operation
    loggerService?.logCompletionTime(now, 'Fetching document');
  }
}