tryAddLocalDocumentReference<T, E> method
Adds a local document reference field to the map if specified conditions are met.
This method is used to maintain a local reference to the Firestore document without storing it in the database. It's particularly useful for maintaining document references in DTOs and models.
This method is used to maintain a local reference to the Firestore document without storing it in the database. It's particularly useful for maintaining document references in DTOs and models.
Example:
final data = {'name': 'John'};
final docRef = FirebaseFirestore.instance.collection('users').doc('123');
final result = data.tryAddLocalDocumentReference(
docRef,
referenceFieldName: '_ref',
Parameters:
- [documentReference] The Firestore document reference to add
- [referenceFieldName] The field name under which to store the reference
- [tryAddLocalDocumentReference] Whether to attempt adding the reference
Returns:
A new [Map] with the document reference added if conditions are met
tryAddLocalDocumentReference: true,
);
Implementation
Map<T, E> tryAddLocalDocumentReference<T, E>(
DocumentReference documentReference, {
required String referenceFieldName,
required bool tryAddLocalDocumentReference,
}) =>
tryAddLocalDocumentReference && containsKey(referenceFieldName)
? this as Map<T, E>
: (this..[referenceFieldName] = documentReference) as Map<T, E>;