json - 我厌倦了在颤抖中找出我的产品列表长度。你能帮我找出我的问题吗?
问题描述
我收到NoSuchMethodError:在 null 上调用了 getter 'length'。接收方:空。尝试在此结构上调用:长度错误。
基本上我试图从 json 文件中找出产品列表并打印产品的长度。它给出了正确的响应,但是当我尝试通过 fromjson 方法将 json 转换为 dart 时,我遇到了上述问题。我通过在线 dart 到 json 转换器制作了我的模型类。所以你能帮我找出我的问题吗?这是我的存储库部分。
Future<List<Products>> getProducts() async {
final response = await http.post(AppStrings.productsListUrl,
body: {"store_uid": "demotest", "APIKey": "test03"});
if (response.statusCode == 200) {
print("success!!!!!");
var data = json.decode(response.body);
List<Products> products = Data.fromJson(data).products;
print(products.length);
return products;
} else {
print("Error");
throw Exception();
}}
这是我的模型课
class ApiResultModel {
String status;
int code;
Data data;
List<String> message;
ApiResultModel({this.status, this.code, this.data, this.message});
ApiResultModel.fromJson(Map<String, dynamic> json) {
status = json['status'];
code = json['code'];
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
message = json['message'].cast<String>();
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
data['code'] = this.code;
if (this.data != null) {
data['data'] = this.data.toJson();
}
data['message'] = this.message;
return data;
}
}
class Data {
Paginator paginator;
int paginationLastPage;
List<Products> products;
Data({this.paginator, this.paginationLastPage, this.products});
Data.fromJson(Map<String, dynamic> json) {
paginator = json['paginator'] != null
? new Paginator.fromJson(json['paginator'])
: null;
paginationLastPage = json['pagination_last_page'];
if (json['products'] != null) {
products = new List<Products>();
json['products'].forEach((v) {
products.add(new Products.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.paginator != null) {
data['paginator'] = this.paginator.toJson();
}
data['pagination_last_page'] = this.paginationLastPage;
if (this.products != null) {
data['products'] = this.products.map((v) => v.toJson()).toList();
}
return data;
}
}
class Paginator {
int currentPage;
int totalPages;
Null previousPageUrl;
String nextPageUrl;
int recordPerPage;
Paginator(
{this.currentPage,
this.totalPages,
this.previousPageUrl,
this.nextPageUrl,
this.recordPerPage});
Paginator.fromJson(Map<String, dynamic> json) {
currentPage = json['current_page'];
totalPages = json['total_pages'];
previousPageUrl = json['previous_page_url'];
nextPageUrl = json['next_page_url'];
recordPerPage = json['record_per_page'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['current_page'] = this.currentPage;
data['total_pages'] = this.totalPages;
data['previous_page_url'] = this.previousPageUrl;
data['next_page_url'] = this.nextPageUrl;
data['record_per_page'] = this.recordPerPage;
return data;
}
}
class Products {
String productName;
String productSku;
String productSkuCode;
String checkoutRefCode;
String productDescription;
int price;
int priceInclTax;
String productThumbImage;
int productTypeId;
int isActive;
int isStockable;
int priceExclTax;
String storeName;
BrandInfo brandInfo;
Products(
{this.productName,
this.productSku,
this.productSkuCode,
this.checkoutRefCode,
this.productDescription,
this.price,
this.priceInclTax,
this.productThumbImage,
this.productTypeId,
this.isActive,
this.isStockable,
this.priceExclTax,
this.storeName,
this.brandInfo});
Products.fromJson(Map<String, dynamic> json) {
productName = json['product_name'];
productSku = json['product_sku'];
productSkuCode = json['product_sku_code'];
checkoutRefCode = json['checkout_ref_code'];
productDescription = json['product_description'];
price = json['price'];
priceInclTax = json['price_incl_tax'];
productThumbImage = json['product_thumb_image'];
productTypeId = json['product_type_id'];
isActive = json['is_active'];
isStockable = json['is_stockable'];
priceExclTax = json['price_excl_tax'];
storeName = json['store_name'];
brandInfo = json['brand_info'] != null
? new BrandInfo.fromJson(json['brand_info'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['product_name'] = this.productName;
data['product_sku'] = this.productSku;
data['product_sku_code'] = this.productSkuCode;
data['checkout_ref_code'] = this.checkoutRefCode;
data['product_description'] = this.productDescription;
data['price'] = this.price;
data['price_incl_tax'] = this.priceInclTax;
data['product_thumb_image'] = this.productThumbImage;
data['product_type_id'] = this.productTypeId;
data['is_active'] = this.isActive;
data['is_stockable'] = this.isStockable;
data['price_excl_tax'] = this.priceExclTax;
data['store_name'] = this.storeName;
if (this.brandInfo != null) {
data['brand_info'] = this.brandInfo.toJson();
}
return data;
}
}
class BrandInfo {
int id;
String brandName;
String brandCode;
int isActive;
String brandLogo;
String brandUid;
String storeUid;
BrandInfo(
{this.id,
this.brandName,
this.brandCode,
this.isActive,
this.brandLogo,
this.brandUid,
this.storeUid});
BrandInfo.fromJson(Map<String, dynamic> json) {
id = json['id'];
brandName = json['brand_name'];
brandCode = json['brand_code'];
isActive = json['is_active'];
brandLogo = json['brand_logo'];
brandUid = json['brand_uid'];
storeUid = json['store_uid'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['brand_name'] = this.brandName;
data['brand_code'] = this.brandCode;
data['is_active'] = this.isActive;
data['brand_logo'] = this.brandLogo;
data['brand_uid'] = this.brandUid;
data['store_uid'] = this.storeUid;
return data;
}
}
解决方案
您的 Data.fromJson 正在返回null
产品列表。我通常发现在这些情况下返回一个空列表会更好,因此?? List.empty()
如果您愿意,可以添加到行尾。这有助于消除空指针异常,或者在本例中为 NoSuchMethod 异常。
它返回一个空产品列表,因为 json['products']( 在if
您的构造函数中的语句中Data.fromJson
) 为空,因此该列表永远不会被初始化为空列表;
推荐阅读
- navision - 试图在 xml 中的特定元素名称中查找和存储特定数据?
- python - 我有一个元素列表,我想删除特定的重复元素并将其附加到 Python 3.7.2 中现有列表的开头
- node.js - 如何更新 node js 应用程序中的静态文件?
- python - 为机器学习设置 keras 包和 python
- django - 如何在 Django 中使用 woocommerce api 将订单状态正确更新为“已完成”
- azure - 未使用 luis api 3.0 版预览版找到应用程序
- msbuild - 缺少项目类型“PackageReference”所需的属性“OutputPath”
- c++ - MFC 中的 StackPanel
- deep-learning - Chainer 中 NStepBiLSTM 中的 xs 是什么类型?
- azure - 与 Azure Express Route 的 P2S 连接