我尝试从 REST API 加载数据,但我遇到了来自 catch 类型“Null”的消息;不是“Map”类型的子类型
我的代码遇到一些错误,我无法弄清楚,我已经尝试了很多步骤,但它保持静止,我在提供程序中调用的数据正在工作,但在页面中仍然为空,这是代码。有没有办法知道哪个字段返回 null 这是我的模型。
OrderListResponse orderListResponseFromJson(String str) =>
OrderListResponse.fromJson(json.decode(str));
class OrderListResponse {
OrderListResponse({
this.status,
this.data,
});
String? status;
Data? data;
factory OrderListResponse.fromJson(Map<String, dynamic> json) =>
OrderListResponse(
status: json["status"],
data: Data.fromJson(json["data"]),
);
}
class Data {
Data({
this.records,
required this.items,
this.pages,
this.page,
});
int? records;
List<OrderItemList> items;
int? pages;
int? page;
factory Data.fromJson(Map<String, dynamic> json) => Data(
records: json["records"],
items: List<OrderItemList>.from(json["items"]
.map<OrderItemList>((items) => OrderItemList.fromJson(items))),
pages: json["pages"],
page: json["page"],
);
}
class OrderItemList {
OrderItemList({
this.id,
this.orderNumber,
this.total,
this.status,
this.type,
this.storeId,
this.shortDescription,
this.orderRating,
this.createdAt,
this.cico,
this.storeLogoUrl,
});
int? id;
String? orderNumber;
double? total;
Status? status;
Type? type;
int? storeId;
String? shortDescription;
int? orderRating;
DateTime? createdAt;
Cico? cico;
String? storeLogoUrl;
factory OrderItemList.fromJson(Map<String, dynamic> json) => OrderItemList(
id: json["id"],
orderNumber: json["orderNumber"],
total: json["total"].toDouble(),
status: statusValues.map[json["status"]],
type: typeValues.map[json["type"]],
storeId: json["storeId"] == null ? null : json["storeId"],
shortDescription:
json["shortDescription"] == null ? null : json["shortDescription"],
orderRating: json["orderRating"],
createdAt: DateTime.parse(json["createdAt"]),
cico: json["cico"] == null ? null : Cico.fromJson(json["cico"]),
storeLogoUrl:
json["storeLogoUrl"] == null ? null : json["storeLogoUrl"],
);
}
class Cico {
Cico({
this.type,
this.currency,
this.amount,
this.total,
});
String? type;
String? currency;
int? amount;
int? total;
factory Cico.fromJson(Map<String, dynamic> json) => Cico(
type: json["type"] == null ? null : json["type"],
currency: json["currency"] == null ? null : json["currency"],
amount: json["amount"] == null ? null : json["amount"],
total: json["total"] == null ? null : json["total"],
);
}
enum Status { processing, completed, canceled }
final statusValues = EnumValues({
"canceled": Status.canceled,
"completed": Status.completed,
"processing": Status.processing
});
enum Type { delivery }
final typeValues = EnumValues({"delivery": Type.delivery});
class EnumValues<T> {
Map<String, T> map;
Map<T, String>? reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
reverseMap ??= map.map((k, v) => MapEntry(v, k));
return reverseMap!;
}
我有一个
REST API 获取数据:
class OrderListProvider {
HttpHelper httpHelper = HttpHelper()..init(domain: BaseUrlDomain.wingMall);
Future<List<OrderListResponse>?> getOrderList({required int page}) async {
try {
final response =
await httpHelper.get(EndPoint.orderList, queryParam: {"page": page});
if (response.statusCode == 200) {
var res = response.data['data']['items'] as List;
log("Information response data: $res");
return res
.map<OrderListResponse>(
(element) => OrderListResponse.fromJson(element))
.toList();
}
return null;
} catch (error) {
log("Information error: $error");
rethrow;
}
}
提供程序,用于从
i face some errors on my code i cant figure it out, i have tried many step but its stay still, the data i call in provider is working but its still null in the page this is the code.Is there any way to know which field return null here is my model.
OrderListResponse orderListResponseFromJson(String str) =>
OrderListResponse.fromJson(json.decode(str));
class OrderListResponse {
OrderListResponse({
this.status,
this.data,
});
String? status;
Data? data;
factory OrderListResponse.fromJson(Map<String, dynamic> json) =>
OrderListResponse(
status: json["status"],
data: Data.fromJson(json["data"]),
);
}
class Data {
Data({
this.records,
required this.items,
this.pages,
this.page,
});
int? records;
List<OrderItemList> items;
int? pages;
int? page;
factory Data.fromJson(Map<String, dynamic> json) => Data(
records: json["records"],
items: List<OrderItemList>.from(json["items"]
.map<OrderItemList>((items) => OrderItemList.fromJson(items))),
pages: json["pages"],
page: json["page"],
);
}
class OrderItemList {
OrderItemList({
this.id,
this.orderNumber,
this.total,
this.status,
this.type,
this.storeId,
this.shortDescription,
this.orderRating,
this.createdAt,
this.cico,
this.storeLogoUrl,
});
int? id;
String? orderNumber;
double? total;
Status? status;
Type? type;
int? storeId;
String? shortDescription;
int? orderRating;
DateTime? createdAt;
Cico? cico;
String? storeLogoUrl;
factory OrderItemList.fromJson(Map<String, dynamic> json) => OrderItemList(
id: json["id"],
orderNumber: json["orderNumber"],
total: json["total"].toDouble(),
status: statusValues.map[json["status"]],
type: typeValues.map[json["type"]],
storeId: json["storeId"] == null ? null : json["storeId"],
shortDescription:
json["shortDescription"] == null ? null : json["shortDescription"],
orderRating: json["orderRating"],
createdAt: DateTime.parse(json["createdAt"]),
cico: json["cico"] == null ? null : Cico.fromJson(json["cico"]),
storeLogoUrl:
json["storeLogoUrl"] == null ? null : json["storeLogoUrl"],
);
}
class Cico {
Cico({
this.type,
this.currency,
this.amount,
this.total,
});
String? type;
String? currency;
int? amount;
int? total;
factory Cico.fromJson(Map<String, dynamic> json) => Cico(
type: json["type"] == null ? null : json["type"],
currency: json["currency"] == null ? null : json["currency"],
amount: json["amount"] == null ? null : json["amount"],
total: json["total"] == null ? null : json["total"],
);
}
enum Status { processing, completed, canceled }
final statusValues = EnumValues({
"canceled": Status.canceled,
"completed": Status.completed,
"processing": Status.processing
});
enum Type { delivery }
final typeValues = EnumValues({"delivery": Type.delivery});
class EnumValues<T> {
Map<String, T> map;
Map<T, String>? reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
reverseMap ??= map.map((k, v) => MapEntry(v, k));
return reverseMap!;
}
}
And there is my provider I perpare for data from rest api:
class OrderListProvider {
HttpHelper httpHelper = HttpHelper()..init(domain: BaseUrlDomain.wingMall);
Future<List<OrderListResponse>?> getOrderList({required int page}) async {
try {
final response =
await httpHelper.get(EndPoint.orderList, queryParam: {"page": page});
if (response.statusCode == 200) {
var res = response.data['data']['items'] as List;
log("Information response data: $res");
return res
.map<OrderListResponse>(
(element) => OrderListResponse.fromJson(element))
.toList();
}
return null;
} catch (error) {
log("Information error: $error");
rethrow;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论