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>?'
  }

>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']);

Leave a Reply