Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter constructor from json list-string

Hi I cant solve my error.

E/flutter (30343): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type ‘List’ is not a subtype of type ‘List’

class Exercise {
  int id;
  String name;
  List<int> equipmentIds;

  Exercise(this.id, this.name, this.equipmentIds);

  Exercise.fromJson(Map<String, dynamic> json)
      : id = json['id'],
        name = json['name'],
        equipmentIds = json['equipmentIds'];

  Map<String, dynamic> toJson() =>
      {'id': id, 'name': name, 'equipmentIds': equipmentIds};
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use List.from

return Exercise(
  map['id']?.toInt() ?? 0,
  map['name'] ?? '',
  List<int>.from(map['equipmentIds']),
);

It would better to handle null while reading map , can be List<int>.from(map['equipmentIds']?? [] ),

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading