toFirestoreCreate method

Map<String, dynamic> toFirestoreCreate({
  1. bool useServerTimestamp = true,
  2. List<String>? fieldsToExclude,
})

Convert to Firestore document for CREATE operations.

By default, uses server timestamps for fields returned by getCreateTimestampFields and getUpdateTimestampFields.

Parameters:

  • useServerTimestamp: If true, uses FieldValue.serverTimestamp() for timestamp fields. If false, preserves DateTime values as Timestamps (useful for data migration/import).
  • fieldsToExclude: List of field names to exclude from the output.

Example:

// Standard create
await firestore.collection('posts').add(post.toFirestoreCreate());

// Import with specific timestamps
await firestore.collection('posts')
  .add(historicalPost.toFirestoreCreate(useServerTimestamp: false));

Implementation

Map<String, dynamic> toFirestoreCreate({
  bool useServerTimestamp = true,
  List<String>? fieldsToExclude,
}) {
  final json = toJson();
  return _processForContext(
    json,
    SerializationContext.firestore,
    isCreate: true,
    useServerTimestamp: useServerTimestamp,
    fieldsToExclude: fieldsToExclude,
  );
}