tryRemoveLocalDocumentReference<T, E> method
This method is used to clean up local document references that were added for internal use.
Removes a local document reference field from the map if specified conditions are met.
Example:
final data = {'name': 'John', '_ref': docRef};
final result = data.tryRemoveLocalDocumentReference(
referenceFieldName: '_ref',
Parameters:
- [referenceFieldName] The field name of the reference to remove
- [tryRemoveLocalDocumentReference] Whether to attempt removing the reference
Returns:
A new [Map] with the document reference removed if conditions are met
tryRemoveLocalDocumentReference: true,
);
Implementation
/// Removes a local document reference field from the map if specified conditions are met.
///
/// Example:
/// ```dart
/// final data = {'name': 'John', '_ref': docRef};
/// final result = data.tryRemoveLocalDocumentReference(
/// referenceFieldName: '_ref',
///
/// Parameters:
/// - [referenceFieldName] The field name of the reference to remove
/// - [tryRemoveLocalDocumentReference] Whether to attempt removing the reference
///
/// Returns:
/// A new [Map] with the document reference removed if conditions are met
/// tryRemoveLocalDocumentReference: true,
/// );
/// ```
Map<T, E> tryRemoveLocalDocumentReference<T, E>({
required String referenceFieldName,
required bool tryRemoveLocalDocumentReference,
}) =>
tryRemoveLocalDocumentReference && containsKey(referenceFieldName)
? (this..remove(referenceFieldName)) as Map<T, E>
: this as Map<T, E>;