CommentModel.fromPartialJson constructor

CommentModel.fromPartialJson(
  1. MapEntry<String, dynamic> json
)

Creates a CommentModel from a JSON object.

Comments are stored slightly different in Firestore, so instead of creating a true fromJson method, we create a fromPartialJson method that takes a MapEntry<String, Map<String, dynamic>>, as the key is the commentID. Perhaps this is something we can improve in the future.

Implementation

factory CommentModel.fromPartialJson(MapEntry<String, dynamic> json) {
  final values = json.value as Map<String, dynamic>;
  return CommentModel(
      commentID: json.key,
      userID: values['userID'] as String,
      comment: values['comment'] as String,
      commentDate: (values['commentDate'] as Timestamp).toDate(),
      isEdited: values['isEdited'] as bool? ?? false,
      lastEdited: values['lastEdited'] != null
          ? (values['lastEdited'] as Timestamp).toDate()
          : null,
    );
}