【问题标题】:Flutter Error : Exception: type 'int' is not a subtype of type 'String'Flutter 错误:异常:“int”类型不是“String”类型的子类型
【发布时间】:2020-04-23 13:20:26
【问题描述】:

它是如何发生的?我认为它发生在我进行 json 映射时。它说异常:类型'int'不是'String'类型的子类型。我试过使用本地 json 资产文件,但没有什么不同。请帮我解决这个问题。

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), () => {});
}

这是我来自 _loadAProductAsset() 函数的 json

[
  {
    "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"
  }
]

【问题讨论】:

    标签: android json flutter dart


    【解决方案1】:

    解决方案 1:您可以将它们定义为动态,而不是将数据类型明确定义为字符串和整数,如下所示:

    dynamic productId;
    dynamic productName;
    dynamic productPrice;
    

    这里发生的情况是,您将负责处理输入的任何数据类型。

    解决方案 2:通过转到浏览器窗口中的链接并查看传入的每组数据的数据类型来检查传入 JSON 的结构。例如。查看 productId 的类型。如果表示为“12”,那么它必须是一个字符串。

    确定您的 JSON 项目的数据类型后,您可以在反序列化 JSON 并在工厂构造函数(在您的情况下为 fromProduct)中定义变量的同时解析数据。如下:

    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;
    }
    

    【讨论】:

      【解决方案2】:

      您需要将来自 JSON 的 String 解析为 int:

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

      【讨论】:

        【解决方案3】:

        由于某种原因,我遇到了这个问题,我之前已经做过json.decode(response),但在做MyModel.fromJson(response)之前我不得不再做一次

        所以我通常推荐的是

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

        以上对我有用。

        【讨论】:

          猜你喜欢
          • 2021-05-10
          • 2020-08-09
          • 1970-01-01
          • 1970-01-01
          • 2020-04-19
          • 2020-09-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多