findCollection method

Query<Map<String, dynamic>> findCollection()

Finds a CollectionReference of type Map<String, dynamic> based on specified _collectionPath.

If _tryAddLocalId is true then your data will also contain a local id field based on the _idFieldName specified in the constructor.

If you rather want to retrieve data in the form of T consider using the findCollectionWithConverter method instead.

Implementation

Query<Map<String, dynamic>> findCollection() {
  _log.info(
    message: 'Finding collection..',
    sensitiveData: _shouldNotSensitiveInfo
        ? null
        : SensitiveData(
            path: _collectionPath(),
          ),
  );
  return (_isCollectionGroup
          ? _firebaseFirestore.collectionGroup(_collectionPath())
          : _firebaseFirestore.collection(_collectionPath()))
      .withConverter<Map<String, dynamic>>(
    fromFirestore: (snapshot, _) {
      final data = snapshot.data() ?? {};
      try {
        return data
            .tryAddLocalId(
              snapshot.id,
              idFieldName: _idFieldName,
              tryAddLocalId: _tryAddLocalId,
            )
            .tryAddLocalDocumentReference(
              snapshot.reference,
              referenceFieldName: _documentReferenceFieldName,
              tryAddLocalDocumentReference: _tryAddLocalDocumentReference,
            );
      } catch (error) {
        _log.error(
          message:
              'Unexpected error caught while adding local id and document reference',
          sensitiveData: _shouldNotSensitiveError
              ? null
              : SensitiveData(
                  path: _collectionPath(),
                  id: snapshot.id,
                  data: data,
                ),
        );
        rethrow;
      }
    },
    toFirestore: (data, _) {
      try {
        return data
            .tryRemoveLocalId(
              idFieldName: _idFieldName,
              tryRemoveLocalId: _tryAddLocalId,
            )
            .tryRemoveLocalDocumentReference(
              referenceFieldName: _documentReferenceFieldName,
              tryRemoveLocalDocumentReference: _tryAddLocalDocumentReference,
            );
      } catch (error) {
        _log.error(
          message: 'Could not find collection',
          sensitiveData: _shouldNotSensitiveError
              ? null
              : SensitiveData(
                  path: _collectionPath(),
                ),
        );
        rethrow;
      }
    },
  );
}