2

I have a JSON like that

 [  
    {
        "id": "2a5",
        "employeeNumber": "101",
        "firstName": "Sachin",
        "lastName": "Agrawal"
    },
    {
        "id": "1f7",
        "employeeNumber": "151",
        "firstName": "Karsten",
        "lastName": "Andersen"
    },
]

I tried to parse the JSON like that

List<Employee> employees = (jsonDecode(response.data) as List)
          .map((data) => Employee.fromJson(data))
          .toList();

it causes the following error

List<dynamic>' is not a subtype of type 'String'

when I try to decode the JSON it causes the following error

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'String' 

what is the proper way to parse this type of JSON?

1
  • could you include result of print(jsonDecode(response.data));? Commented Oct 13, 2022 at 5:49

1 Answer 1

2

You don't need to decode your response.data, it is already in list form. So instead of this:

List<Employee> employees = (jsonDecode(response.data) as List)
          .map((data) => Employee.fromJson(data))
          .toList();

try this:

List<Employee> employees = (response.data as List)
          .map((data) => Employee.fromJson(data))
          .toList();
Sign up to request clarification or add additional context in comments.

Comments

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.