BaseFirestoreModel class abstract

Base class for all Firestore models with enhanced serialization capabilities.

This base class provides intelligent serialization methods that handle different contexts (Firestore, Cloud Functions, local storage) and automatically manages timestamp fields.

Key Features:

  • Automatic server timestamp handling for create/update operations
  • Support for data migration with explicit timestamps
  • Separate handling of creation vs update timestamps
  • Cloud Functions compatible serialization
  • Flexible field exclusion

Usage:

@JsonSerializable(explicitToJson: true)
class PostModel extends BaseFirestoreModel {
  final String id;
  final String title;

  @SmartTimestampConverter()
  final DateTime? createdAt;

  @SmartTimestampConverter()
  final DateTime? updatedAt;

  PostModel({
    this.id = '',
    required this.title,
    this.createdAt,
    this.updatedAt,
  });

  factory PostModel.fromJson(Map<String, dynamic> json) => _$PostModelFromJson(json);

  @override
  Map<String, dynamic> toJson() => _$PostModelToJson(this);
}

Creating Documents:

// Standard create with server timestamps
await firestore.collection('posts')
  .add(newPost.toFirestoreCreate());

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

Updating Documents:

// Update (preserves createdAt, updates updatedAt)
await firestore.collection('posts')
  .doc(postId)
  .update(updatedPost.toFirestoreUpdate());

Cloud Functions:

// Sending data to callable function (removes timestamp fields)
final callable = FirebaseFunctions.instance.httpsCallable('createPost');
await callable.call(post.toCallable());

// Receiving data from callable function
final result = await callable.call({'postId': 'abc123'});
final post = PostModel.fromJson(result.data as Map<String, dynamic>);

The SmartTimestampConverter automatically handles all timestamp formats returned by Cloud Functions, including {_seconds, _nanoseconds} maps, milliseconds, and ISO strings.

Implementers

Constructors

BaseFirestoreModel()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

getCreateTimestampFields() List<String>
Fields that should only be set on document creation (like createdAt).
getUpdateTimestampFields() List<String>
Fields that should always be updated with server timestamp (like updatedAt).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
postProcessJson(Map<String, dynamic> json, SerializationContext context) Map<String, dynamic>
Override to add custom post-processing for specific models.
toCallable() Map<String, dynamic>
Convert for Firebase callable functions.
toFirestore({bool isUpdate = false}) Map<String, dynamic>
Legacy method for Firestore conversion.
toFirestoreCreate({bool useServerTimestamp = true, List<String>? fieldsToExclude}) Map<String, dynamic>
Convert to Firestore document for CREATE operations.
toFirestoreRaw({List<String>? fieldsToExclude}) Map<String, dynamic>
Convert to Firestore with explicit control (advanced use).
toFirestoreUpdate({List<String>? fieldsToExclude}) Map<String, dynamic>
Convert to Firestore document for UPDATE operations.
toJson() Map<String, dynamic>
The single generated toJson method from json_serializable. This should be implemented by the generated code.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited