1
List<String> langList = [English,  Gujarati,  Hindi,  Marathi,  Punjabi,  Urdu,  Spanish]

var selectedLanguagesList = <LanguageDatum>[].obs;

langList is a string type of list. and selectedLanguagesList is a model type of list. How do I convert string list to model list in flutter ?

class LanguageDatum {
  LanguageDatum({
     this.id,
     this.name,
     this.status,
    this.createdAt,
    this.updatedAt,
    this.deletedAt,
  });

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

  factory LanguageDatum.fromJson(Map<String, dynamic> json) => LanguageDatum(
    id: json["id"]??0,
    name: json["name"]??"",
    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,
    "name": name,
    "status": status,
    "created_at": createdAt,
    "updated_at": updatedAt,
    "deleted_at": deletedAt,
  };
}

So, Above is model class.

2 Answers 2

1

If I understand your question correctly, I guess it would be

// To convert String array to Model array
final listOfLanguageDatum =
    langList.map((e) => LanguageDatum(name: e)).toList();

List<String> langList = [
  'Tamil'
  'English',
  'Gujarati',
  'Hindi',
  'Marathi',
  'Punjabi',
  'Urdu',
  'Spanish'
];

class LanguageDatum extends GetxController {
  LanguageDatum({
    this.id,
    this.name,
    this.status,
    this.createdAt,
    this.updatedAt,
    this.deletedAt,
  });

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

  factory LanguageDatum.fromJson(Map<String, dynamic> json) => LanguageDatum(
        id: json["id"] ?? 0,
        name: json["name"] ?? "",
        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,
        "name": name,
        "status": status,
        "created_at": createdAt,
        "updated_at": updatedAt,
        "deleted_at": deletedAt,
      };

  @override
  String toString() {
    return 'LanguageDatum(id: $id, name: $name, status: $status, createdAt: $createdAt, updateAt: $updatedAt, deletedAt: $deletedAt)';
  }
}

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

7 Comments

electedLanguagesList ::: [Instance of 'LanguageDatum', Instance of 'LanguageDatum']
I passed this, selectedLanguagesList.value = split.map((e) => LanguageDatum(name: e.toString())).toList().obs;
I think you can assign e.name to the name variable?
e is itself string. so we can't pass e.name.toString();
emm, can I take a look at your Class?
|
0

Right now your language is of String type, to make language object You need to make a model class say,

Class LanguageDatum{
String name;
   ...
}

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.