2

How it be happend ? i think it happend when i doin json mapping. it said Exception: type 'int' is not a subtype of type 'String'. I've tried using local json assets file but it not make different. Please help me with this problem.

class Product {
  int productId;
  String productName;
  String productPrice;

  Product({this.productId, this.productName, this.productPrice});

  Product.fromProduct(Product p) {
    this.productId = p.productId;
    this.productName = p.productName;
    this.productPrice = p.productPrice;
  }

  factory Product.fromJson(Map<String, dynamic> parsedJson) {
    return Product(
        productId: parsedJson['ID'],
        productName: parsedJson['Name'],
        productPrice: parsedJson['SellPrice']);
  }
}

Future<String> _loadAProductAsset() async {
  var res = await http.get(Uri.encodeFull("http://10.0.2.2:9155/product"));
  return json.encode(json.decode(res.body)["data"]);
}

List<Product> parseProduct(String myJson) {
  final parsed = json.decode(myJson).cast<Map<String, dynamic>>();
  return parsed.map<Product>((json) => Product.fromJson(json)).toList();
}

Future<List<Product>> fetchProduct() async {
  await wait(1);
  String jsonString = await _loadAProductAsset();
  return compute(parseProduct, jsonString);
}

Future wait(int s) {
  return new Future.delayed(Duration(seconds: s), () => {});
}

this is my json from _loadAProductAsset() function

[
  {
    "ID": 2,
    "CreatedAt": "2020-01-06T03:56:32+07:00",
    "UpdatedAt": "2020-01-06T03:56:32+07:00",
    "DeletedAt": null,
    "Name": "Product A",
    "Category": "0",
    "Stock": "50",
    "StockUnit": "0",
    "BuyPrice": "20000",
    "SellPrice": "21000",
    "SupplierID": "1"
  }
]

3 Answers 3

2

Solution 1: Instead of explicitly defining the data types as string and ints you could define them as dynamics as follow:

dynamic productId;
dynamic productName;
dynamic productPrice;

What happens here is that you are giving the responsibility to dart of taking care of casting whatever data type that comes in.

Solution 2: Check the structure of incoming JSON by going to the link in your browser window and seeing what is the Data type for each set of data that is coming in. For eg. seeing what is the type of productId. If it is stated as "12" then it must be a string.

After concluding the data type of your JSON items, you could parse the data while deserialising the JSON and defining the variables in the factory constructor(fromProduct in your case). This goes as follows:

Product.fromProduct(Product p) {
  this.productId = int.parse(p.productId); // takes in a String and converts it into an int.
  this.productName = p.productName;
  this.productPrice = p.productPrice;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to parse the String that comes from JSON into an int:

Product.fromProduct(Product p) {
  this.productId = int.parse(p.productId);
  this.productName = p.productName;
  this.productPrice = p.productPrice;
}

Comments

0

For some reason I had this issue and I already did json.decode(response) earlier but I had to do it again just before doing MyModel.fromJson(response)

So what I typically recommend is

import 'dart:convert';
.
.
.
.
var decodedJson = json.decode(decode);
MyModel model = MyModel.fromJson(decodedJson);

The above worked for me.

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.