2

enter image description hereenter image description hereenter image description hereI have one Json structure with three maps, one is list of products then total and tax. I have to parse this json structure in flutter. I created on model class. Now i am getting the error in type casting.

How to solve this?

JSON Structure:

{
  "products" : [
     {                  
       "cart_id": "7",      
     },          
     {                  
       "cart_id": "7",
     }     
  ],
  "total": 100,
  "tax": 100
}

Model class :

class CartModel {   
    List<Product> produtcts;
    double total;

    CartModel({this.produtcts, this.total});

    factory CartModel.fromJson(Map<String, dynamic> json) {
        var list = json['products'] as List;
        print(list.runtimeType);
        List<Product> products = list.map((i) => 
           Product.fromJson(i)).toList();

        return CartModel(
            produtcts: products, total: json['total'],);
    }
}

class Product {
    String cartId;

    Product({this.cartId,});

    factory Product.fromJson(Map<String, dynamic> json) {
        return Product(     
            productId: json['cart_id'],
        );
    }
}
2
  • where are you error, Product.fromJson or CartModel.fromJson ? Commented Jun 26, 2019 at 11:56
  • The error is in cartmodel : type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast Commented Jun 26, 2019 at 12:00

2 Answers 2

5

Instead of casting the products array to a list try using it as an Iterable.

For me the following code works (note that the json.decode(String) method is imported from the dart:convert package):

var data = '{"products":[{"cart_id": "7"},{ "cart_id": "7"}], "total": 100, "tax": 100}';
var decoded = json.decode(data);   
var cartModel = CartModel.fromJson(decoded);

class CartModel {   
    List<Product> produtcts;
    int total;

    CartModel({this.produtcts, this.total});

    factory CartModel.fromJson(Map<String, dynamic> json) {
        Iterable list = json['products'];
        print(list.runtimeType);
        List<Product> products = list.map((i) => 
           Product.fromJson(i)).toList();

        return CartModel(
            produtcts: products, total: json['total'],);
    }
}

class Product {
    String productId;

    Product({this.productId,});

    factory Product.fromJson(Map<String, dynamic> json) {
        return Product(     
            productId: json['cart_id'],
        );
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

The same error, _InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast.
@proversion I have updated my answer with an example on how I called the CartModel.fromJson method. This code is working fine, just note that I had to correct some syntax errors in your original question (I have also fixed these in the original question).
final data = jsonDecode(response.body); final rest = data as List; list = rest.map<CartModel>((json) => CartModel.fromJson(json)).toList();
Did you check the result you received from your backend system (the actual JSON)? Does it contain a value for the array? I have tried using jsonDecode() in the above code and works just fine. To me it looks like the json you are trying to parse is slightly different from what you are expecting and thus resulting in the error.
Map( 3 Items) - 0 -> Products, 1->total , 2 - > tax
|
1

Quite simply :

 List<CartModel> list = new List();
  var jsonlist = jsonDecode(yourJsonString) as List;
  jsonlist.forEach((e) {
    list.add(CartModel.fromJson(e));
  });

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.