Home > Software design >  Flutter constructor from json list-string
Flutter constructor from json list-string

Time:10-29

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

CodePudding user response:

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']?? [] ),

  • Related