2

I have model type of list, That list is used to collect id and speciality of user. specialityList is a list.

var specialityList = <SpecialityDatum>[].obs;

SpecialityDatum is Model,

class SpecialityDatum {
  SpecialityDatum({
     this.id,
     this.speciality,
     this.status,
    this.createdAt,
    this.updatedAt,
    this.deletedAt,
  });

  int? id;
  String? speciality;
  int? status;
  DateTime? createdAt;
  DateTime? updatedAt;
  dynamic deletedAt;

  factory SpecialityDatum.fromJson(Map<String, dynamic> json) => SpecialityDatum(
    id: json["id"]??0,
    speciality: json["speciality"]??"",
    status: json["status"]??"",
    createdAt: json["created_at"] != null ? DateTime.parse(json["created_at"]) : null,
    updatedAt: json["updated_at"] != null ? DateTime.parse(json["updated_at"]) : null,
    deletedAt: json["deleted_at"],
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "speciality": speciality,
    "status": status,
    "created_at": createdAt,
    "updated_at": updatedAt,
    "deleted_at": deletedAt,
  };

  String toStringView(){ // what ever you name it
    return '$speciality'; // if you want to show id and speciality as string
  }

}

I want to convert that model list into array, like below,...

Output : [{"speciality_id":12,"speciality_name":"orthopedic"},{"speciality_id":13,"speciality_name":"hyoerthing"}]
1
  • Usex a for loop to iterate over the items in output and add them to a list. Commented Sep 17, 2022 at 6:50

3 Answers 3

1

Try this:

var result = specialityList.map((e) => e.toJson()).toList();

result would be a list of map that contain Speciality like id, status and other.

Sign up to request clarification or add additional context in comments.

Comments

0

there is an easy and short way to do this is:

spouse you have out put in list of map: var output=[{"speciality_id":12,"speciality_name":"orthopedic"},{"speciality_id":13,"speciality_name":"hyoerthing"}] var listofObj= output.map((e) => e.toJson()).toList();

Comments

0

Try this

class SpecialityDatum {
  SpecialityDatum({
     this.id,
     this.speciality,
     this.status,
    this.createdAt,
    this.updatedAt,
    this.deletedAt,
  });

  int? id;
  String? speciality;
  int? status;
  DateTime? createdAt;
  DateTime? updatedAt;
  dynamic deletedAt;

  factory SpecialityDatum.fromJson(Map<String, dynamic> json) => SpecialityDatum(
    id: json["id"]??0,
    speciality: json["speciality"]??"",
    status: json["status"]??"",
    createdAt: json["created_at"] != null ? DateTime.parse(json["created_at"]) : null,
    updatedAt: json["updated_at"] != null ? DateTime.parse(json["updated_at"]) : null,
    deletedAt: json["deleted_at"],
  );

   Map<String, dynamic> toJson() => {
    if(id != null) "speciality_id": id, // changed
    if(speciality != null)  "speciality_name": speciality, // changed
    if(status != null)  "status": status, // changed
    if(createdAt != null)  "created_at": createdAt, // changed
    if(updatedAt != null)  "updated_at": updatedAt, // changed
    if(deletedAt != null)  "deleted_at": deletedAt, // changed
  };

  String toStringView(){ // what ever you name it
    return '$speciality'; // if you want to show id and speciality as string
  }

}

and then you need to map it to new list like:

var mapedSpecialityList = specialityList.map((e) => e.toJson()).toList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.