findById method

Future<FeedbackResponse<Map<String, dynamic>>> findById({
  1. required String id,
  2. String? collectionPathOverride,
})

Finds a document based on given id.

This method returns raw data in the form of a Map<String, dynamic>. If _tryAddLocalId is true then the map will also contain a local id field based on the _idFieldName specified in the constructor so you may retrieve document id's more easily after serialization.

If you rather want to convert this data into T immediately you should use the findByIdWithConverter method instead. Make sure to have specified the _toJson and _fromJson methods or else the FirestoreApi will not know how to convert the data to T.

Implementation

Future<FeedbackResponse<Map<String, dynamic>>> findById({
  required String id,
  String? collectionPathOverride,
}) async {
  assert(
    _isCollectionGroup == (collectionPathOverride != null),
    'Firestore does not support finding a document by id when communicating with a collection group, '
    'therefore, you must specify the collectionPathOverride containing all parent collection and document ids '
    'in order to make this method work.',
  );
  try {
    _log.info(
      message: 'Finding without converter..',
      sensitiveData: _shouldNotSensitiveInfo
          ? null
          : SensitiveData(
              path: collectionPathOverride ?? _collectionPath(),
              id: id,
            ),
    );
    final result = (await findDocRef(
      id: id,
      collectionPathOverride: collectionPathOverride,
    ).get(_getOptions))
        .data();
    if (result != null) {
      _log.success(
        message: 'Found item!',
        sensitiveData: null,
      );
      return _responseConfig.searchSuccessResponse(
        isPlural: false,
        result: result,
      );
    } else {
      _log.warning(
        message: 'Found nothing!',
        sensitiveData: null,
      );
      return _responseConfig.searchFailedResponse(isPlural: false);
    }
  } catch (error, stackTrace) {
    _log.error(
      message: 'Unable to find document',
      sensitiveData: _shouldNotSensitiveError
          ? null
          : SensitiveData(
              path: collectionPathOverride ?? _collectionPath(),
              id: id,
            ),
      error: error,
      stackTrace: stackTrace,
    );
    return _responseConfig.searchFailedResponse(isPlural: false);
  }
}