0

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

2
  • could you include your api response too? Commented Oct 29, 2022 at 10:06
  • can you please add the json string you receive as code? Commented Oct 29, 2022 at 10:06

1 Answer 1

1

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

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

1 Comment

Worked with equipmentIds = List<int>.from(json['equipmentIds']); My List is not nullable so I always just pass an empty list [].

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.