我正在使用一個在應用程式啟動時回傳空串列的 API。它在 particuler 行中顯示以下錯誤: productList: List<ProductList>.from(json["ProductList"].map((x) => ProductList.fromJson(x))),
我該如何處理這個錯誤?我看到了一些解決方案,他們提供了一個空串列 []。如果你只是告訴我我應該在這里使用什么語法來處理這種錯誤......
對于此錯誤,我的應用程式有時會無限加載。

我的模型類
import 'package:meta/meta.dart';
import 'dart:convert';
MyCartItemListModel myCartItemListModelFromJson(String str) =>
MyCartItemListModel.fromJson(json.decode(str));
String myCartItemListModelToJson(MyCartItemListModel data) =>
json.encode(data.toJson());
class MyCartItemListModel {
MyCartItemListModel({
required this.status,
required this.message,
required this.userId,
required this.cookieVal,
required this.totalItem,
required this.cartProductTotal,
required this.shippingCharge,
required this.cartTotal,
required this.productList,
});
int status;
String message;
String userId;
String cookieVal;
String totalItem;
String cartProductTotal;
String shippingCharge;
String cartTotal;
List<ProductList> productList;
factory MyCartItemListModel.fromJson(Map<String, dynamic> json) =>
MyCartItemListModel(
status: json["Status"],
message: json["Message"],
userId: json["UserId"] ??"",
cookieVal: json["CookieVal"],
totalItem: json["TotalItem"],
cartProductTotal: json["CartProductTotal"],
shippingCharge: json["ShippingCharge"],
cartTotal: json["CartTotal"],
productList: List<ProductList>.from(json["ProductList"].map((x) => ProductList.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"Status": status,
"Message": message,
"UserId": userId,
"CookieVal": cookieVal,
"TotalItem": totalItem,
"CartProductTotal": cartProductTotal,
"ShippingCharge": shippingCharge,
"CartTotal": cartTotal,
"ProductList": List<dynamic>.from(productList.map((x) => x.toJson())),
};
}
class ProductList {
ProductList({
required this.cartId,
required this.productId,
required this.productName,
required this.productImage,
required this.productSize,
required this.productColor,
required this.productRate,
required this.quantity,
required this.productTotal,
});
String cartId;
String productId;
String productName;
String productImage;
String productSize;
String productColor;
String productRate;
String quantity;
String productTotal;
factory ProductList.fromJson(Map<String, dynamic> json) => ProductList(
cartId: json["CartId"],
productId: json["ProductId"],
productName: json["ProductName"],
productImage: json["ProductImage"],
productSize: json["ProductSize"],
productColor: json["ProductColor"],
productRate: json["ProductRate"],
quantity: json["Quantity"],
productTotal: json["ProductTotal"],
);
Map<String, dynamic> toJson() => {
"CartId": cartId,
"ProductId": productId,
"ProductName": productName,
"ProductImage": productImage,
"ProductSize": productSize,
"ProductColor": productColor,
"ProductRate": productRate,
"Quantity": quantity,
"ProductTotal": productTotal,
};
}
我的 json 回應:
{
"Status": 1,
"Message": "",
"UserId": "2",
"CookieVal": "",
"TotalItem": "4",
"CartProductTotal": "1767",
"ShippingCharge": "50",
"CartTotal": "1817",
"ProductList": [
{
"CartId": "450",
"ProductId": "10622",
"ProductName": " Kids Baby Leggings Pink",
"ProductImage": "https://sleepkart.co.in/productimage/zb9diak47ocm0q957itf_1.jpg",
"ProductSize": "L",
"ProductColor": "#fdc291",
"ProductRate": "190",
"Quantity": "1",
"ProductTotal": "190"
},
{
"CartId": "449",
"ProductId": "10623",
"ProductName": "Kids Baby Leggings Green",
"ProductImage": "https://sleepkart.co.in/productimage/ogr137q1kjr9fiqwdipd_1.jpg",
"ProductSize": "L",
"ProductColor": "#42d19a",
"ProductRate": "193",
"Quantity": "1",
"ProductTotal": "193"
},
{
"CartId": "438",
"ProductId": "10661",
"ProductName": "Night Suit for Women",
"ProductImage": "https://sleepkart.co.in/productimage/4jcrpnqw655vg7yoyvun_1.jpg",
"ProductSize": "L",
"ProductColor": "#f2be02",
"ProductRate": "975",
"Quantity": "1",
"ProductTotal": "975"
},
{
"CartId": "437",
"ProductId": "10575",
"ProductName": "Men's Navy Blue Bermuda",
"ProductImage": "https://sleepkart.co.in/productimage/zn8oqvspajuks9u1pre4_1.jpg",
"ProductSize": "FREE",
"ProductColor": "#0c1155",
"ProductRate": "409",
"Quantity": "1",
"ProductTotal": "409"
}
]
}
uj5u.com熱心網友回復:
你可以這樣檢查:
productList: json["ProductList"] == null ? [] :
List<ProductList>.from(json["ProductList"].map((x)
=>ProductList.fromJson(x)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382753.html
上一篇:為不同的JSON檔案撰寫類
下一篇:不可滾動時展開的ListView
