skip to Main Content

Here is my Product class:

class Product {
  int? id ;
  String? shop_id;
  String? category_id;
  String? sub_cat_id;
  String? product_title;
  String? product_Description;
  String? product_image;
  int? product_price;
  int? discount;
  int? quantity;
  int? uom_id;
  String? in_stock;
  String? status;
  String? created_at;
  String? updated_at;
  String? imagename;
  String? store_name;
  String? uom_name;

  Product({required this.id, required this.shop_id,this.category_id,this.sub_cat_id, this.product_title,
    this.product_Description,this.product_image,this.product_price, this.discount, this.quantity, this.uom_id, this.in_stock,
    this.created_at, this.updated_at, this.imagename, this.store_name, this.uom_name
  });

  factory Product.fromJson(Map<String, dynamic> json) {
    return Product(id: json['id'] as int, shop_id: json['shop_id'] as String,category_id: json['category_id'] as String,sub_cat_id: json['sub_cat_id'] as String,product_title: json['product_title'] as String,
        product_Description: json['product_Description'] as String, product_image:json['product_image'] as String, product_price: json['product_price']as int,
        discount: json['discount'] as int, quantity: json['quantity'] as int, uom_id: json['uom_id'] as int, in_stock: json['in_stock'] as String,
        created_at: json['created_at'] as String, updated_at: json['updated_at'] as String, imagename: json['store_name'] as String, uom_name: json['uom_name']as String,
    );
  }
}

My function to return a list of products is

Future<List<Product>> fetchProducts() async {



http.Response response = await http.get(
  Uri.parse(productUrl + 'product_details/8'),
  headers: {
    HttpHeaders.acceptHeader: 'application/json',
    HttpHeaders.authorizationHeader: 'Bearer $token'
  },
);



Product user = Product.fromJson(jsonDecode(response.body)['data']);
print(user.toString());
final List<Product> loadedProducts = [];

final extractedData = json.decode(response.body)['data'];


extractedData.forEach((prodId, prodData) {

  print('about to add');

  loadedProducts.add(Product(
    id: int.parse(prodId),
    shop_id: prodData.shop_id,
    category_id: prodData['category_id'],
    sub_cat_id: prodData['sub_cat_id'],
    product_title: prodData['product_title'],
    product_Description:prodData['product_Description'],
    product_image:prodData['product_image'],
    product_price: prodData['product_price'],
    discount: prodData['discount'],
    quantity: prodData['quantity'],
    uom_id: prodData['uom_id'],
    in_stock: prodData['in_stock'],
    created_at: prodData['created_at'],
    updated_at: prodData['updated_at'],
    imagename: prodData['store_name'],
    uom_name: prodData['uom_name'],

  ));
});
print(loadedProducts.length);

List products = jsonDecode(response.body)['data'];


return jsonDecode(response.body)['data'].map((product) => Product.fromJson(product)).toList();



 }

I am trying to parse data from a remote API, and then convert the JSON data to a list of products. I keep getting the error

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type ‘String’ is not a subtype of type ‘int’ in type cast

What am I doing wrong?

2

Answers


  1. Instead of using as use int.tryParse to all int field like,

    id: int.tryParse("${json['id']}"),
    
    Login or Signup to reply.
  2. the (int) could be null, so instead of parse use tryParse. and if it

    Example:

     int.tryParse("${json['id']}"),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search