findByIdWithConverter method
Finds a document based on given id
.
This method returns data in the form of type T
. Make sure to have specified the _toJson
and
_fromJson
methods or else the FirestoreApi will not know how to convert the data to T
.
If _tryAddLocalId
is true then your data will also contain a local id field based
on the _idFieldName
specified in the constructor. Add this id field to your T
and you will
have easy access to the document id at any time.
If _tryAddLocalDocumentReference
is true then your data will also contain a local reference field based
on the _documentReferenceFieldName
specified in the constructor. Add this reference field to your T
and you will
have easy access to the document reference at any time.
If you rather want to retrieve data in the raw form of a Map<String, dynamic> consider using the findById method instead.
Implementation
Future<FeedbackResponse<T>> findByIdWithConverter({
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('🔥 Finding ${collectionPathOverride ?? _collectionPath()} '
'with converter, '
'id: $id..');
final result = (await findDocRefWithConverter(
id: id,
collectionPathOverride: collectionPathOverride,
).get())
.data();
if (result != null) {
_log.success('🔥 Found item!');
return _responseConfig.searchSuccessResponse(isPlural: false, result: result);
} else {
_log.warning('🔥 Found nothing!');
return _responseConfig.searchFailedResponse(isPlural: false);
}
} catch (error, stackTrace) {
_log.error(
'🔥 Unable to find ${_collectionPath()} document with converter and id: $id.',
error: error,
stackTrace: stackTrace,
);
return _responseConfig.searchFailedResponse(isPlural: false);
}
}