import 'dart:convert'; ChallengesResponseModel challengesResponseModelFromJson(String str) => ChallengesResponseModel.fromJson(json.decode(str)); String challengesResponseModelToJson(ChallengesResponseModel data) => json.encode(data.toJson()); // Helper function for safe integer conversion int? _safeToInt(dynamic value) { if (value == null) return null; if (value is int) return value; if (value is double) return value.toInt(); if (value is String) { return int.tryParse(value); } return null; } class ChallengesResponseModel { bool? isSuccess; int? statusCode; String? message; Data? data; dynamic errors; ChallengesResponseModel({ this.isSuccess, this.statusCode, this.message, this.data, this.errors, }); factory ChallengesResponseModel.fromJson(Map json) => ChallengesResponseModel( isSuccess: json["isSuccess"], statusCode: _safeToInt(json["statusCode"]), message: json["message"], data: json["data"] == null ? null : Data.fromJson(json["data"]), errors: json["errors"], ); Map toJson() => { "isSuccess": isSuccess, "statusCode": statusCode, "message": message, "data": data?.toJson(), "errors": errors, }; } class Data { int? totalCount; List? items; Data({this.totalCount, this.items}); factory Data.fromJson(Map json) => Data( totalCount: _safeToInt(json["totalCount"]), items: json["items"] == null ? [] : List.from( json["items"]!.map((x) => ChallengeItem.fromJson(x)), ), ); Map toJson() => { "totalCount": totalCount, "items": items == null ? [] : List.from(items!.map((x) => x.toJson())), }; } class ChallengeItem { int? challengeId; String? challengeTitle; String? challengeDescription; DateTime? startDate; DateTime? endDate; String? challengeImageName; String? challengeVideoName; String? challengeThumbnailName; int? fitnessGoalId; int? challengeVisibilityID; String? fitnessGoalTitle; int? totalParticipants; bool? isChallengeJoined; bool? isChallengeStarted; bool? isChallengeEnded; List? tasks; List? participants; ChallengeItem({ this.challengeId, this.challengeTitle, this.challengeDescription, this.startDate, this.endDate, this.challengeImageName, this.challengeVideoName, this.challengeThumbnailName, this.fitnessGoalId, this.challengeVisibilityID, this.fitnessGoalTitle, this.totalParticipants, this.isChallengeJoined, this.tasks, this.participants, this.isChallengeStarted, this.isChallengeEnded, }); factory ChallengeItem.fromJson(Map json) => ChallengeItem( challengeId: _safeToInt(json["challengeID"]), challengeTitle: json["challengeTitle"], challengeDescription: json["challengeDescription"], startDate: json["startDate"] == null ? null : DateTime.tryParse(json["startDate"]), endDate: json["endDate"] == null ? null : DateTime.tryParse(json["endDate"]), challengeImageName: json["challengeImageName"], challengeVideoName: json["challengeVideoName"], challengeThumbnailName: json["challengeThumbnailName"], fitnessGoalId: _safeToInt(json["fitnessGoalID"]), fitnessGoalTitle: json["fitnessGoalTitle"], totalParticipants: _safeToInt(json["totalParticipants"]), isChallengeJoined: json["isChallengeJoined"], isChallengeEnded: json["isChallengeEnded"], isChallengeStarted: json["isChallengeStarted"], challengeVisibilityID: json["challengeVisibilityID"], tasks: json["tasks"] == null ? [] : List.from( json["tasks"]!.map((x) => ChallengeTask.fromJson(x)), ), participants: json["participants"] == null ? [] : List.from( json["participants"]!.map((x) => Participant.fromJson(x)), ), ); Map toJson() => { "challengeID": challengeId, "challengeTitle": challengeTitle, "challengeDescription": challengeDescription, "startDate": startDate?.toIso8601String(), "endDate": endDate?.toIso8601String(), "challengeImageName": challengeImageName, "challengeVideoName": challengeVideoName, "challengeThumbnailName": challengeThumbnailName, "fitnessGoalID": fitnessGoalId, "fitnessGoalTitle": fitnessGoalTitle, "totalParticipants": totalParticipants, "isChallengeJoined": isChallengeJoined, "isChallengeStarted": isChallengeStarted, "isChallengeEnded": isChallengeEnded, "challengeVisibilityID": challengeVisibilityID, "tasks": tasks == null ? [] : List.from(tasks!.map((x) => x.toJson())), "participants": participants == null ? [] : List.from(participants!.map((x) => x.toJson())), }; } class ChallengeTask { int? challengeTaskId; String? taskTitle; int? taskMetricId; String? taskUnit; int? targetValue; int? challengeId; String? createdByUserId; ChallengeTask({ this.challengeTaskId, this.taskTitle, this.taskMetricId, this.taskUnit, this.targetValue, this.challengeId, this.createdByUserId, }); factory ChallengeTask.fromJson(Map json) => ChallengeTask( challengeTaskId: _safeToInt(json["challengeTaskID"]), taskTitle: json["taskTitle"], taskMetricId: _safeToInt(json["taskMetricID"]), taskUnit: json["taskUnit"], targetValue: _safeToInt(json["targetValue"]), challengeId: _safeToInt(json["challengeID"]), createdByUserId: json["createdByUserID"], ); Map toJson() => { "challengeTaskID": challengeTaskId, "taskTitle": taskTitle, "taskMetricID": taskMetricId, "taskUnit": taskUnit, "targetValue": targetValue, "challengeID": challengeId, "createdByUserID": createdByUserId, }; } class Participant { int? challengeParticipationId; String? userId; String? firstName; String? lastName; String? fullName; int? userTypeId; String? userTypeName; String? profilePicture; String? status; DateTime? joinedAt; DateTime? leftAt; Participant({ this.challengeParticipationId, this.userId, this.firstName, this.lastName, this.fullName, this.userTypeId, this.userTypeName, this.profilePicture, this.status, this.joinedAt, this.leftAt, }); factory Participant.fromJson(Map json) => Participant( challengeParticipationId: _safeToInt(json["challengeParticipationID"]), userId: json["userID"], firstName: json["firstName"], lastName: json["lastName"], fullName: json["fullName"], userTypeId: _safeToInt(json["userTypeId"]), userTypeName: json["userTypeName"], profilePicture: json["profilePicture"], status: json["status"], joinedAt: json["joinedAt"] == null ? null : DateTime.tryParse(json["joinedAt"]), leftAt: json["leftAt"] == null ? null : DateTime.tryParse(json["leftAt"]), ); Map toJson() => { "challengeParticipationID": challengeParticipationId, "userID": userId, "firstName": firstName, "lastName": lastName, "fullName": fullName, "userTypeId": userTypeId, "userTypeName": userTypeName, "profilePicture": profilePicture, "status": status, "joinedAt": joinedAt?.toIso8601String(), "leftAt": leftAt?.toIso8601String(), }; }