Flutter 错误:flatter/lib/ui/ui_dart_state.cc(186)]未处理的异常:type';字符串';不是类型为';int';
我正在尝试为登录用户获取购物车,但当我点击购物车屏幕时,这就是我在控制台中遇到的错误类型,错误如下所示Flutter 错误:flatter/lib/ui/ui_dart_state.cc(186)]未处理的异常:type';字符串';不是类型为';int';,flutter,dart,Flutter,Dart,我正在尝试为登录用户获取购物车,但当我点击购物车屏幕时,这就是我在控制台中遇到的错误类型,错误如下所示 [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'String' is not a subtype of type 'int' class CartItem { CartItem({ this.id, this.owner_id, this.user_id,
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'String' is not a subtype of type 'int'
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,
};
}
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))),
factory CartResponse.fromJson(映射json)=>CartResponse(
name:json[“name”]==null?null:json[“name”],
所有者id:json[“所有者id”]==null?null:json[“所有者id”],
购物车项目:json[“购物车项目”]==null?null:List.from(json[“购物车项目”].map((x)=>CartItem.fromJson(x)),
))
有错误的那一行是上面代码中的第三行,我想消除错误cartItem类或定义如下
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'String' is not a subtype of type 'int'
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{
卡蒂姆({
这个身份证,
这是你的身份证,
此.user\u id,
此.product\u id,
这个产品的名称,
此.product\u缩略图\u图像,
这是一种变异,
这个.U符号,
这个价格,,
这个,税收,,
这是运费,
这个数量,,
这个,下限,
这个上限,,
});
int-id;
int所有者id;
int用户标识;
int产品标识;
字符串产品名称;
字符串产品\u缩略图\u图像;
串变;
双倍价格;
字符串货币符号;
双重征税;
运费加倍;
整数;
int下限;
int上限;
工厂CartItem.fromJson(映射json)=>CartItem(
id:json[“id”]==null?null:json[“id”],
所有者id:json[“所有者id”]==null?null:json[“所有者id”],
user\u id:json[“user\u id”]==null?null:json[“user\u id”],
产品标识:json[“产品标识”]==null?null:json[“产品标识”],
产品名称:json[“产品名称”]==null?null:json[“产品名称”],
产品缩略图图像:json[“产品缩略图图像”]==null?null:json[“产品缩略图图像”],
variation:json[“variation”]==null?null:json[“variation”],
price:json[“price”]==null?null:json[“price”].toDouble(),
货币符号:json[“货币符号”]==null?null:json[“货币符号”],
tax:json[“tax”]==null?null:json[“tax”].toDouble(),
配送成本:json[“配送成本”]==null?null:json[“配送成本”]。toDouble(),
数量:json[“数量”]==null?null:json[“数量”],
下限:json[“下限”]==null?null:json[“下限”],
上限:json[“上限”]==null?null:json[“上限”],
);
映射到JSON()=>{
“id”:id==null?null:id,
“所有者id”:所有者id==null?null:所有者id,
“user\u id”:user\u id==null?null:user\u id,
“产品标识”:产品标识==null?null:产品标识,
“产品名称”:产品名称==null?null:产品名称,
“产品缩略图图像”:产品缩略图图像==null?null:产品缩略图图像,
“变量”:变量==null?null:变量,
“价格”:价格==null?null:价格,
“货币符号”:货币符号==null?null:货币符号,
“tax”:tax==null?null:tax,
“运输成本”:运输成本==null?null:运输成本,
“数量”:数量==null?null:数量,
“下限”:下限==null?null:下限,
“上限”:上限==null?null:上限,
};
}
您应该将整数值转换为字符串。
例如
int number;
Text(int.toString());
json[“owner\u 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))),
factory CartResponse.fromJson(映射json)=>CartResponse(
name:json[“name”]==null?null:json[“name”],
所有者id:json[“所有者id”]==null?null:int.parse(json[“所有者id”]),
购物车项目:json[“购物车项目”]==null?null:List.from(json[“购物车项目”].map((x)=>CartItem.fromJson(x)),
我们需要将字符串
s从json
映射转换为int
s和double
s:
// 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\u response.dart
工厂CartResponse.fromJson(映射json)=>CartResponse(
name:json[“name”]==null?null:json[“name”],
所有者id:json[“所有者id”]==null?null:int.parse(json[“所有者id”]),
购物车项目:json[“购物车项目”]==null?null:List.from(json[“购物车项目”].map((x)=>CartItem.fromJson(x)),
);
//购物车项目类定义
类CartItem{
卡蒂姆({
这个身份证,
这是你的身份证,
此.user\u id,
此.product\u id,
这个产品的名称,
此.product\u缩略图\u图像,
这是一种变异,
这个.U符号,
这个价格,,
这个,税收,,
这是运费,
这个数量,,
这个,下限,
这个上限,,
});
int-id;
int所有者id;
int用户标识;
int产品标识;
字符串产品名称;
字符串产品\u缩略图\u图像;
串变;
双倍价格;
字符串货币符号;
双重征税;
运费加倍;
整数;
int下限;
int上限;
工厂CartItem.fromJson(映射json)=>CartItem(
id:json[“id”]==null?null:int.parse(json[“id”]),
所有者id:json[“所有者id”]==null?null:int.parse(json[“所有者id”]),
user\u id:json[“user\u id”]==null?null:int.parse(json[“user\u id”]),
产品id:json[“产品id”]==null?null:int.parse(json[“产品id”]),
产品名称:json[“产品名称”]==null?null:json[“产品名称”],
产品缩略图图像:json[“产品缩略图图像”]==null?null:json[“产品缩略图图像”],
variation:json[“variation”]==null?null:json[“variation”],
price:json[“price”]==null?null:double.parse(json[“price”]),
货币符号:json[“货币符号”]==null?null:json[“货币符号”],
tax:json[“tax”]==null?null:double.parse(json[“tax”]),
配送成本:json[“配送成本”]==null?null:double.parse(json[“配送成本”]),
数量:json[“数量”]==null?null:int.parse(json[“数量”]),
下限:json[“下限”]==null?null:int.parse(json[“下限”]),
上限:json[“上限”]==null?nul