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

type 'String' is not a subtype of type 'List<int>?' – flutter/dart

I’m receiving the following error type ‘String’ is not a subtype of type ‘List?’ while converting the map to BarModel object

despite receiving the list of int from response, it is throwing type ‘String’ is not a subtype of type ‘List? when assigning it to the openDays

List<int>? openDays;

BarModel.fromMap(Map<String, dynamic> json) {
    print(json['open_days']); // [1, 2, 3]
    barId = json['bar_id'];
    barName = json['bar_name'];
    barAddress = json['bar_address'];
    openDays = json['open_days']; // error type 'String' is not a subtype of type 'List<int>?'
  }

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 :

Assuming that the Map you are inputting is the actual result of a jsonDecode of a JSON string it must mean that the JSON was actually formatted like

{
  "open_days" : "[1, 2, 3]"
  ...
}

instead of this

{
  "open_days" : [1, 2, 3]
  ...
}

which is the correct way if you want it to be an actual list and not a string.

If you have no control over the JSON string you could solve it by jsonDecode that part another time, so like

openDays = jsonDecode(json['open_days']);
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