findByQuery method

Future<FeedbackResponse<List<Map<String, dynamic>>>> findByQuery({
  1. required CollectionReferenceQuery<Map<String, dynamic>> collectionReferenceQuery,
  2. required String whereDescription,
})

Finds documents based on a given collectionReferenceQuery.

Use the whereDescription to describe what your collectionReferenceQuery is looking for so that it shows proper logging in your console.

This method returns raw data in the form of a List<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 a list of T immediately you should use the findByQueryWithConverter 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<List<Map<String, dynamic>>>> findByQuery({
  required CollectionReferenceQuery<Map<String, dynamic>>
      collectionReferenceQuery,
  required String whereDescription,
}) async {
  try {
    _log.info(
      message: 'Finding without converter, with custom query..',
      sensitiveData: _shouldNotSensitiveInfo
          ? null
          : SensitiveData(
              path: _collectionPath(),
              whereDescription: whereDescription,
            ),
    );
    final result = (await collectionReferenceQuery(
      findCollection(),
    ).get(_getOptions))
        .docs
        .map(
          (e) => e.data(),
        )
        .toList();
    _logResultLength(result);
    return _responseConfig.searchSuccessResponse(
      isPlural: result.isPlural,
      result: result,
    );
  } catch (error, stackTrace) {
    _log.error(
      message: 'Unable to find documents with custom query',
      sensitiveData: _shouldNotSensitiveError
          ? null
          : SensitiveData(
              path: _collectionPath(),
              whereDescription: whereDescription,
            ),
      error: error,
      stackTrace: stackTrace,
    );
    return _responseConfig.searchFailedResponse(isPlural: true);
  }
}