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};

>Solution :

You can use List.from

return Exercise(
  map['id']?.toInt() ?? 0,
  map['name'] ?? '',

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

Leave a ReplyCancel reply