错误:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:“String”类型不是“int”类型的子类型

时间:2021-05-09 09:16:45

标签: flutter dart

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

inf

cart_response.dart

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

);

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

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))),

3 个答案:

答案 0 :(得分:1)

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))),

答案 1 :(得分:1)

我们需要将 String 映射中的 json 转换为 intdouble

// 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 :(得分:0)

您应该将整数值转换为字符串。 例子..

int number;
Text(int.toString());