How to convert model type of list to Map<String, List> element in flutter?

Viewed 47

I have model type of list.

Which is RxList<ConsultationRequestDatum> pastConsultationList =
      <ConsultationRequestDatum>[].obs

Now, I want to convert it into

Map<String, List> _elements = {
  'appointmentDate': ['id', 'appointment_time', 'name','age'], //appointmentDate = "Today"
  'appointmentDate': ['id', 'appointment_time', 'name','age'], //appointmentDate = "Yesterday"
  'appointmentDate': ['id', 'appointment_time', 'name','age'], //appointmentDate = "4 september 2022"
  'appointmentDate': ['id', 'appointment_time', 'name','age'], //appointmentDate = "5 september 2022"
  'appointmentDate': ['id', 'appointment_time', 'name','age']  //appointmentDate = "6 september 2022"
  };

How do I do it in flutter ?

ConsulationRequestDatum is model. It is too long that's why I did not upload. I just want to convert list to map<String, List> _elements. In ConsulationRequestDatum model I have date,time,text,...and other variables. but in _elements I want date as key and other informations as values. Thank you

class ConsultationRequestDatum {
  ConsultationRequestDatum({
    this.id,
    this.appointmentId,
    this.appointmentDate,
    this.appointmentTime,
    this.appointmentDateTime,
    this.consultationMemberId,
    this.status,
    this.userId,
    this.name,
    this.age,
    this.gender,
    this.notificationGroupTitle
  });

  int? id;
  String? appointmentId;
  DateTime? appointmentDate;
  String? appointmentTime;
  DateTime? appointmentDateTime;
  int? consultationMemberId;
  int? status;
  int? userId;
  String? name;
  String? age;
  String? gender;
  String? notificationGroupTitle;

  factory ConsultationRequestDatum.fromJson(Map<String, dynamic> json) => ConsultationRequestDatum(
    id: json["id"],
    appointmentId: json["appointment_id"],
    appointmentDate: DateTime.parse(json["appointment_date"]),
    appointmentTime: json["appointment_time"],
    appointmentDateTime: DateTime.parse(json["appointment_date_time"]),
    consultationMemberId: json["consultation_member_id"],
    status: json["status"],
    userId: json["user_id"],
    name: json["name"],
    age: json["age"],
    gender: json["gender"],
    notificationGroupTitle: json["notificationGroupTitle"] == null ? null : json["notificationGroupTitle"],
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "appointment_id": appointmentId,
    "appointment_date": "${appointmentDate!.year.toString().padLeft(4, '0')}-${appointmentDate!.month.toString().padLeft(2, '0')}-${appointmentDate!.day.toString().padLeft(2, '0')}",
    "appointment_time": appointmentTime,
    "appointment_date_time": appointmentDateTime!.toIso8601String(),
    "consultation_member_id": consultationMemberId,
    "status": status,
    "user_id": userId,
    "name": name,
    "age": age,
    "gender": gender,
    "notificationGroupTitle": notificationGroupTitle,
  };

}

enter image description here

appointmentDate will be used as header(date) in listview. you can see that in image. and other things are used in section items file in listview.

0 Answers
Related