【问题标题】:ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'String' is not a subtype of type 'int'错误:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:类型 'String' 不是类型 'int' 的子类型
【发布时间】:2021-05-09 09:16:45
【问题描述】:

我正在尝试为登录用户获取购物车,但是当我点击购物车屏幕时,这是我在控制台中遇到的错误,错误如下

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'String' is not a subtype of type 'int'

cart_response.dart

factory CartResponse.fromJson(Map<String, dynamic> json) => CartResponse(
name: json["name"] == null ? null : json["name"],
owner_id: json["owner_id"] == null ? null : json["owner_id"],
cart_items: json["cart_items"] == null ? null : List<CartItem>.from(json["cart_items"].map((x) => CartItem.fromJson(x))),

);

出现错误的行是上面代码中的第三行,我想摆脱错误,cartItem 类或定义如下

    class CartItem {
  CartItem({
    this.id,
    this.owner_id,
    this.user_id,
    this.product_id,
    this.product_name,
    this.product_thumbnail_image,
    this.variation,
    this.currency_symbol,
    this.price,
    this.tax,
    this.shipping_cost,
    this.quantity,
    this.lower_limit,
    this.upper_limit,
  });

  int id;
  int owner_id;
  int user_id;
  int product_id;
  String product_name;
  String product_thumbnail_image;
  String variation;
  double price;
  String currency_symbol;
  double tax;
  double shipping_cost;
  int quantity;
  int lower_limit;
  int upper_limit;

  factory CartItem.fromJson(Map<String, dynamic> json) => CartItem(
    id: json["id"] == null ? null : json["id"],
    owner_id: json["owner_id"] == null ? null : json["owner_id"],
    user_id: json["user_id"] == null ? null : json["user_id"],
    product_id: json["product_id"] == null ? null : json["product_id"],
    product_name: json["product_name"] == null ? null : json["product_name"],
    product_thumbnail_image: json["product_thumbnail_image"] == null ? null : json["product_thumbnail_image"],
    variation: json["variation"] == null ? null : json["variation"],
    price: json["price"] == null ? null : json["price"].toDouble(),
    currency_symbol: json["currency_symbol"] == null ? null : json["currency_symbol"],
    tax: json["tax"] == null ? null : json["tax"].toDouble(),
    shipping_cost: json["shipping_cost"] == null ? null : json["shipping_cost"].toDouble(),
    quantity: json["quantity"] == null ? null : json["quantity"],
    lower_limit: json["lower_limit"] == null ? null : json["lower_limit"],
    upper_limit: json["upper_limit"] == null ? null : json["upper_limit"],
  );

  Map<String, dynamic> toJson() => {
    "id": id == null ? null : id,
    "owner_id": owner_id == null ? null : owner_id,
    "user_id": user_id == null ? null : user_id,
    "product_id": product_id == null ? null : product_id,
    "product_name": product_name == null ? null : product_name,
    "product_thumbnail_image": product_thumbnail_image == null ? null : product_thumbnail_image,
    "variation": variation == null ? null : variation,
    "price": price == null ? null : price,
    "currency_symbol": currency_symbol == null ? null : currency_symbol,
    "tax": tax == null ? null : tax,
    "shipping_cost": shipping_cost == null ? null : shipping_cost,
    "quantity": quantity == null ? null : quantity,
    "lower_limit": lower_limit == null ? null : lower_limit,
    "upper_limit": upper_limit == null ? null : upper_limit,
  };
}

【问题讨论】:

  • 您能发布CartItem 类定义吗?它肯定会有所帮助!谢谢
  • 添加了 cartItem 类定义

标签: flutter dart


【解决方案1】:

我们需要将Strings 从json 映射转换为ints 和doubles:

// cart_response.dart
factory CartResponse.fromJson(Map<String, dynamic> json) => CartResponse(
   name: json["name"] == null ? null : json["name"],
   owner_id: json["owner_id"] == null ? null : int.parse(json["owner_id"]),
   cart_items: json["cart_items"] == null ? null : List<CartItem>.from(json["cart_items"].map((x) => CartItem.fromJson(x))),
);
// cart item class definition
class CartItem {
  CartItem({
    this.id,
    this.owner_id,
    this.user_id,
    this.product_id,
    this.product_name,
    this.product_thumbnail_image,
    this.variation,
    this.currency_symbol,
    this.price,
    this.tax,
    this.shipping_cost,
    this.quantity,
    this.lower_limit,
    this.upper_limit,
  });

  int id;
  int owner_id;
  int user_id;
  int product_id;
  String product_name;
  String product_thumbnail_image;
  String variation;
  double price;
  String currency_symbol;
  double tax;
  double shipping_cost;
  int quantity;
  int lower_limit;
  int upper_limit;

  factory CartItem.fromJson(Map<String, dynamic> json) => CartItem(
    id: json["id"] == null ? null : int.parse(json["id"]),
    owner_id: json["owner_id"] == null ? null : int.parse(json["owner_id"]),
    user_id: json["user_id"] == null ? null : int.parse(json["user_id"]),
    product_id: json["product_id"] == null ? null : int.parse(json["product_id"]),
    product_name: json["product_name"] == null ? null : json["product_name"],
    product_thumbnail_image: json["product_thumbnail_image"] == null ? null : json["product_thumbnail_image"],
    variation: json["variation"] == null ? null : json["variation"],
    price: json["price"] == null ? null : double.parse(json["price"]),
    currency_symbol: json["currency_symbol"] == null ? null : json["currency_symbol"],
    tax: json["tax"] == null ? null : double.parse(json["tax"]),
    shipping_cost: json["shipping_cost"] == null ? null : double.parse(json["shipping_cost"]),
    quantity: json["quantity"] == null ? null : int.parse(json["quantity"]),
    lower_limit: json["lower_limit"] == null ? null : int.parse(json["lower_limit"]),
    upper_limit: json["upper_limit"] == null ? null : int.parse(json["upper_limit"]),
  );

  Map<String, dynamic> toJson() => {
    "id": id == null ? null : id,
    "owner_id": owner_id == null ? null : owner_id,
    "user_id": user_id == null ? null : user_id,
    "product_id": product_id == null ? null : product_id,
    "product_name": product_name == null ? null : product_name,
    "product_thumbnail_image": product_thumbnail_image == null ? null : product_thumbnail_image,
    "variation": variation == null ? null : variation,
    "price": price == null ? null : price,
    "currency_symbol": currency_symbol == null ? null : currency_symbol,
    "tax": tax == null ? null : tax,
    "shipping_cost": shipping_cost == null ? null : shipping_cost,
    "quantity": quantity == null ? null : quantity,
    "lower_limit": lower_limit == null ? null : lower_limit,
    "upper_limit": upper_limit == null ? null : upper_limit,
  };
}

【讨论】:

    【解决方案2】:

    json["owner_id"] 是字符串格式。所以你必须把它转换成int

    factory CartResponse.fromJson(Map<String, dynamic> json) => CartResponse(
    name: json["name"] == null ? null : json["name"],
    owner_id: json["owner_id"] == null ? null : int.parse(json["owner_id"]),
    cart_items: json["cart_items"] == null ? null : List<CartItem>.from(json["cart_items"].map((x) => CartItem.fromJson(x))),
    

    【讨论】:

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