0

I have this Json. I want to map my json data. But i get some error.

{
"Id": 0,
"Product_Id": 0,
"Quantity": 0,
"User_Id": "a49a10d2-fc3f-477a-b087-5b0d07545964",
"Active": false,
"CartProducts": null,
"Products": [
    {
        "Id": 116,
        "Shop_Id": 1,
        "Offer": 0.0,
        "Quantity": 1,
        "Price": 100.0,
        "Category_Id": 0,
        "Description": null,
        "Name": "Lacoste Product",
        "Active": false,
        "Size": "small",
        "Color": "black",
        "Is_External_Product": true,
        "External_Link": "https://www.lacoste.com.tr/urun/kadin-kirmizi-polo-pf0504-007-4/",
        "Currency": null,
        "ProductImages": null
    }
]
} 

I am decoding Json here

if(jsonObject['Products']!=null){
  productItems = ProductItem.getListFromJson(jsonObject['Products']);
}

static List<ProductItem> getListFromJson(List<dynamic> jsonArray) {
log("getListFromJson");
List<ProductItem> list = [];
for (int i = 0; i < jsonArray.length; i++) {
  list.add(ProductItem.fromJson(jsonArray[i]));
}
return list;
 }

But i get this error. "[log] type 'List' is not a subtype of type 'String'"

3
  • show your model Commented Jun 4, 2021 at 6:53
  • Do you get a line where the error occurs? Because jsonArray[i] is of type dynamic and you are passing it into your fromJson function. What are you supposed to pass into the fromJson? A List or does it expect a String? Commented Jun 4, 2021 at 6:53
  • i am passing a list (Products). I have a list in a json object. Commented Jun 4, 2021 at 7:13

2 Answers 2

1

You are trying to assign a value of type String to the product items array. There might be a possibility one of your responses is returning a String and you are expecting an array. Please inspect the response.

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

Comments

0

It's a JSON parsing issue, and it might not directly related to "Products" as being a list or string.

So instead, for better diagnosing this error, or other error on the future, you could need to enable the dart debugger, while checking the "uncaught exception" option, which will guide you to the broken type casting issue.

Enable dart debugger for uncaught exceptions

2 Comments

How can i open this setting window?
@CihanKalmaz If you are using the Visual Studio Code, you can use the ( ⌘ + ⇧ + D) or open the run & Debug from the left side menu.

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.