withCreatedAtAndUpdatedAt<T, E> method

Map<T, E> withCreatedAtAndUpdatedAt<T, E>({
  1. required String createdAtFieldName,
  2. required String updatedAtFieldName,
})

This method is used to automatically track both creation and last update times. The field names are configurable through the Firestore API setup.

Adds both 'created' and 'updatedAt' timestamp fields to the map.

Example:

final data = {'name': 'John'};
final result = data.withCreatedAndUpdated(
  createdAtFieldName: 'createdAt',

Parameters:
- [createdAtFieldName] The field name under which to store the creation timestamp
- [updatedAtFieldName] The field name under which to store the update timestamp

Returns:
A new [Map] with both created and updated timestamps added
  updatedAtFieldName: 'updatedAt',
);

Implementation

/// Adds both 'created' and 'updatedAt' timestamp fields to the map.
///
/// Example:
/// ```dart
/// final data = {'name': 'John'};
/// final result = data.withCreatedAndUpdated(
///   createdAtFieldName: 'createdAt',
///
/// Parameters:
/// - [createdAtFieldName] The field name under which to store the creation timestamp
/// - [updatedAtFieldName] The field name under which to store the update timestamp
///
/// Returns:
/// A new [Map] with both created and updated timestamps added
///   updatedAtFieldName: 'updatedAt',
/// );
/// ```
Map<T, E> withCreatedAtAndUpdatedAt<T, E>({
  required String createdAtFieldName,
  required String updatedAtFieldName,
}) {
  final now = Timestamp.now();
  return (this
    ..[createdAtFieldName] = now
    ..[updatedAtFieldName] = now) as Map<T, E>;
}