Comment.fromJson constructor

Comment.fromJson(
  1. Map<String, dynamic> json, {
  2. required String? currentUserId,
  3. List<Comment>? replies,
})

Creates a Comment from a JSON map.

The JSON should contain the following fields:

  • id: String (required)
  • date: ISO 8601 date string (required)
  • parentId: String? (optional, for replies)
  • content: String (required)
  • entityId: String (required)
  • userId: String (required)
  • username: String (required)
  • likeCount: int? (optional, defaults to 0)
  • replyCount: int? (optional, defaults to 0)
  • isLiked: bool? (optional, defaults to false)

json is the JSON map to parse. currentUserId is used to determine if isMine should be true. replies is an optional list of replies to include.

Returns a new Comment instance.

Implementation

factory Comment.fromJson(Map<String, dynamic> json, {required String? currentUserId, List<Comment>? replies}) {
  return Comment(
    id: json['id'],
    date: DateTime.parse(json['date']),
    parentId: json['parentId'],
    content: json['content'],
    entityId: json['entityId'],
    userId: json['userId'],
    username: json['username'],
    likeCount: json['likeCount'] ?? 0,
    replyCount: json['replyCount'] ?? 0,
    isLiked: json['isLiked'] ?? false,
    isMine: currentUserId != null && currentUserId == json['userId'],
    replies: replies ?? [],
  );
}