import 'dart:convert'; import 'package:intl/intl.dart'; import 'package:aitrainer_app/model/customer_training_plan_details.dart'; class CustomerTrainingPlan { int? customerTrainingPlanId; int? customerId; int? trainingPlanId; DateTime? dateAdd; bool? active; String? status; String? name; CustomerTrainingPlan(); List details = []; CustomerTrainingPlan.fromJson(Map json) { this.customerTrainingPlanId = json['customerTrainingPlanId']; this.customerId = json['customerId']; this.trainingPlanId = json['trainingPlanId']; this.dateAdd = DateTime.parse(json['dateAdd']); this.active = json['active']; this.status = json['status']; this.name = json['name']; } CustomerTrainingPlan.fromJsonWithDetails(Map json) { this.customerTrainingPlanId = json['customerTrainingPlanId']; this.customerId = json['customerId']; this.trainingPlanId = json['trainingPlanId']; this.dateAdd = json['dateAdd'] != null ? DateTime.parse(json['dateAdd']) : DateTime.now(); this.active = json['active']; this.status = json['status']; this.name = json['name']; try { final String details = json['details']; String jsonDetails = details.replaceAllMapped( RegExp(r'([a-zA-Z]+|[0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})'), (Match m) => "\"${m[0]}\""); jsonDetails = jsonDetails.replaceAll(r'\"null\"', 'null'); jsonDetails = jsonDetails.replaceAll(r'\"false\"', 'false'); jsonDetails = jsonDetails.replaceAll(r'\"true\"', 'true'); Iterable iterable = jsonDecode(jsonDetails); this.details = iterable.map((detail) => CustomerTrainingPlanDetails.fromJsonWithExerciseList(detail)).toList(); } on Exception catch (e) { print("JsonDecode error " + e.toString()); } } Map toJson() => { "customerTrainingPlanId": this.customerTrainingPlanId, "customerId": this.customerId, "trainingPlanId": this.trainingPlanId, "dateAdd": DateFormat('yyyy-MM-dd HH:mm:ss').format(this.dateAdd!), "name": this.name, "active": this.active, "status": this.status }; Map toJsonWithDetails() => { "customerTrainingPlanId": this.customerTrainingPlanId, "customerId": this.customerId, "trainingPlanId": this.trainingPlanId, "dateAdd": DateFormat('yyyy-MM-dd HH:mm:ss').format(this.dateAdd!), "name": this.name, "active": this.active, "status": this.status, 'details': details.isEmpty ? [].toString() : details.map((detail) => detail.toJsonWithExercises()).toList().toString(), }; @override String toString() => this.toJson().toString(); }