Home > Net >  How to convert model type of list to array in flutter?
How to convert model type of list to array in flutter?

Time:09-17

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"}]

CodePudding user response:

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.

CodePudding user response:

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();

CodePudding user response:

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();

  • Related