findCollection method
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('🔥 Finding ${_collectionPath()} CollectionReference without converter..');
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(
'🔥 Unexpected ${error.runtimeType} caught while serializing ${_collectionPath()} '
'with id: ${snapshot.id} and '
'data: $data',
);
rethrow;
}
},
toFirestore: (data, _) {
try {
return data
.tryRemoveLocalId(
idFieldName: _idFieldName,
tryRemoveLocalId: _tryAddLocalId,
)
.tryRemoveLocalDocumentReference(
referenceFieldName: _documentReferenceFieldName,
tryRemoveLocalDocumentReference: _tryAddLocalDocumentReference,
);
} catch (error) {
_log.error(
'🔥 Unexpected ${error.runtimeType} caught while deserializing ${_collectionPath()} and '
'data: $data',
);
rethrow;
}
},
);
}