findOne method

Future findOne(
  1. dynamic collectionName,
  2. dynamic docId
)

Implementation

Future findOne(collectionName, docId) async {
  Map<String, dynamic>? data = {};
  try {
    await _firestore.collection(collectionName).doc(docId).get().then((doc) {
      if (doc.exists) {
        data = doc.data();
      } else {
        if (kDebugMode) {
          print('Document $docId does not exist.');
        }
      }
      return doc;
    });
    if (data == {}) {
      return null;
    } else {
      return data;
    }
  } catch (e) {
    if (kDebugMode) {
      print('Error fetching document $docId: $e');
    }

    return null;
  }
}