Future<List<Dashboard>> _getDashboard() async {
print(parameters.toString());
var response = await http.post(API.get_dashboard2, body: parameters);
var dashboard = json.decode(response.body);
var _dashboards = dashboard['success'];
if (_dashboards == 1) {
if (response.statusCode == 200) {
for (listJson in dashboard) {
_list.add(Dashboard.fromJson(listJson));
}
}
} else if (_dashboards == 0) {
Toast.show(dashboard['message'], context,
gravity: Toast.BOTTOM, duration: Toast.LENGTH_SHORT);
}
}
我的 Json 响应是:
{"order":[{"total_order":"14","o_cur_month":"0","o_per_month":"4"}],"customer":[{"total_customer":"12","c_cur_month":"0","c_per_month":"1"}],"pending":[{"total_pending_top":"5","total_pending_bottom":"2","cur_month_top":"0","cur_month_bottom":"0","per_month_top":"1","per_month_bottom":"1","six_month_top":"0","six_month_bottom":"0"}],"receiving":[{"total_rec_top":"9","total_rec_bottom":"5","cur_month_top":"0","cur_month_bottom":"0","per_month_top":"2","per_month_bottom":"2","average_sales_tops":"10.0000","average_sales_Bottoms":"6.0000"}],"success":1,"message":"Data Found"}
让我给你我的模型类,从中你可以更正确地理解
class Dashboard {
List<Order> order;
List<Customer> customer;
List<Pending> pending;
List<Receiving> receiving;
int success;
String message;
Dashboard(
{this.order,
this.customer,
this.pending,
this.receiving,
this.success,
this.message});
Dashboard.fromJson(Map<String, dynamic> json) {
if (json['order'] != null) {
order = new List<Order>();
json['order'].forEach((v) {
order.add(new Order.fromJson(v));
});
}
if (json['customer'] != null) {
customer = new List<Customer>();
json['customer'].forEach((v) {
customer.add(new Customer.fromJson(v));
});
}
if (json['pending'] != null) {
pending = new List<Pending>();
json['pending'].forEach((v) {
pending.add(new Pending.fromJson(v));
});
}
if (json['receiving'] != null) {
receiving = new List<Receiving>();
json['receiving'].forEach((v) {
receiving.add(new Receiving.fromJson(v));
});
}
success = json['success'];
message = json['message'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.order != null) {
data['order'] = this.order.map((v) => v.toJson()).toList();
}
if (this.customer != null) {
data['customer'] = this.customer.map((v) => v.toJson()).toList();
}
if (this.pending != null) {
data['pending'] = this.pending.map((v) => v.toJson()).toList();
}
if (this.receiving != null) {
data['receiving'] = this.receiving.map((v) => v.toJson()).toList();
}
data['success'] = this.success;
data['message'] = this.message;
return data;
}
}
class Order {
String totalOrder;
String oCurMonth;
String oPerMonth;
Order({this.totalOrder, this.oCurMonth, this.oPerMonth});
Order.fromJson(Map<String, dynamic> json) {
totalOrder = json['total_order'];
oCurMonth = json['o_cur_month'];
oPerMonth = json['o_per_month'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['total_order'] = this.totalOrder;
data['o_cur_month'] = this.oCurMonth;
data['o_per_month'] = this.oPerMonth;
return data;
}
}
class Customer {
String totalCustomer;
String cCurMonth;
String cPerMonth;
Customer({this.totalCustomer, this.cCurMonth, this.cPerMonth});
Customer.fromJson(Map<String, dynamic> json) {
totalCustomer = json['total_customer'];
cCurMonth = json['c_cur_month'];
cPerMonth = json['c_per_month'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['total_customer'] = this.totalCustomer;
data['c_cur_month'] = this.cCurMonth;
data['c_per_month'] = this.cPerMonth;
return data;
}
}
class Pending {
String totalPendingTop;
String totalPendingBottom;
String curMonthTop;
String curMonthBottom;
String perMonthTop;
String perMonthBottom;
String sixMonthTop;
String sixMonthBottom;
Pending(
{this.totalPendingTop,
this.totalPendingBottom,
this.curMonthTop,
this.curMonthBottom,
this.perMonthTop,
this.perMonthBottom,
this.sixMonthTop,
this.sixMonthBottom});
Pending.fromJson(Map<String, dynamic> json) {
totalPendingTop = json['total_pending_top'];
totalPendingBottom = json['total_pending_bottom'];
curMonthTop = json['cur_month_top'];
curMonthBottom = json['cur_month_bottom'];
perMonthTop = json['per_month_top'];
perMonthBottom = json['per_month_bottom'];
sixMonthTop = json['six_month_top'];
sixMonthBottom = json['six_month_bottom'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['total_pending_top'] = this.totalPendingTop;
data['total_pending_bottom'] = this.totalPendingBottom;
data['cur_month_top'] = this.curMonthTop;
data['cur_month_bottom'] = this.curMonthBottom;
data['per_month_top'] = this.perMonthTop;
data['per_month_bottom'] = this.perMonthBottom;
data['six_month_top'] = this.sixMonthTop;
data['six_month_bottom'] = this.sixMonthBottom;
return data;
}
}
class Receiving {
String totalRecTop;
String totalRecBottom;
String curMonthTop;
String curMonthBottom;
String perMonthTop;
String perMonthBottom;
String averageSalesTops;
String averageSalesBottoms;
Receiving(
{this.totalRecTop,
this.totalRecBottom,
this.curMonthTop,
this.curMonthBottom,
this.perMonthTop,
this.perMonthBottom,
this.averageSalesTops,
this.averageSalesBottoms});
Receiving.fromJson(Map<String, dynamic> json) {
totalRecTop = json['total_rec_top'];
totalRecBottom = json['total_rec_bottom'];
curMonthTop = json['cur_month_top'];
curMonthBottom = json['cur_month_bottom'];
perMonthTop = json['per_month_top'];
perMonthBottom = json['per_month_bottom'];
averageSalesTops = json['average_sales_tops'];
averageSalesBottoms = json['average_sales_Bottoms'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['total_rec_top'] = this.totalRecTop;
data['total_rec_bottom'] = this.totalRecBottom;
data['cur_month_top'] = this.curMonthTop;
data['cur_month_bottom'] = this.curMonthBottom;
data['per_month_top'] = this.perMonthTop;
data['per_month_bottom'] = this.perMonthBottom;
data['average_sales_tops'] = this.averageSalesTops;
data['average_sales_Bottoms'] = this.averageSalesBottoms;
return data;
}
}
这是我的仪表板模型类,它可以让您清楚地知道当我出错时,还请指导我解决这个难题
我是这个社区的新手,所以请多多关照
如果有任何方法可以通过它访问这个 json,那就太好了。
提前致谢。
答案 0 :(得分:0)
我认为在处理 JSON 数据时应该使用模型/DTO 以避免此类错误。
import 'dart:convert';
class DashboardModel {
final List<Order>? order;
final List<Customer>? customer;
final List<Pending>? pending;
final List<Receiving>? receiving;
final int success;
final String message;
DashboardModel({
required this.order,
required this.customer,
required this.pending,
required this.receiving,
required this.success,
required this.message,
});
DashboardModel copyWith({
List<Order>? order,
List<Customer>? customer,
List<Pending>? pending,
List<Receiving>? receiving,
int? success,
String? message,
}) {
return DashboardModel(
order: order ?? this.order,
customer: customer ?? this.customer,
pending: pending ?? this.pending,
receiving: receiving ?? this.receiving,
success: success ?? this.success,
message: message ?? this.message,
);
}
Map<String, dynamic> toMap() {
return {
'order': order?.map((x) => x.toMap()).toList(),
'customer': customer?.map((x) => x.toMap()).toList(),
'pending': pending?.map((x) => x.toMap()).toList(),
'receiving': receiving?.map((x) => x.toMap()).toList(),
'success': success,
'message': message,
};
}
factory DashboardModel.fromMap(Map<String, dynamic> map) {
return DashboardModel(
order: List<Order>.from(
List.from(map['order']).map(
(x) => Order.fromMap(x),
),
),
customer: List<Customer>.from(
List.from(map['customer']).map(
(x) => Customer.fromMap(x),
),
),
pending: List<Pending>.from(
List.from(map['pending']).map(
(x) => Pending.fromMap(x),
),
),
receiving: List<Receiving>.from(
List.from(map['receiving']).map(
(x) => Receiving.fromMap(x),
),
),
success: map['success'],
message: map['message'],
);
}
String toJson() => json.encode(toMap());
factory DashboardModel.fromJson(String source) =>
DashboardModel.fromMap(json.decode(source));
}
class Order {
final String total_order;
final String o_cur_month;
final String o_per_month;
Order({
required this.total_order,
required this.o_cur_month,
required this.o_per_month,
});
Order copyWith({
String? total_order,
String? o_cur_month,
String? o_per_month,
}) {
return Order(
total_order: total_order ?? this.total_order,
o_cur_month: o_cur_month ?? this.o_cur_month,
o_per_month: o_per_month ?? this.o_per_month,
);
}
Map<String, dynamic> toMap() {
return {
'total_order': total_order,
'o_cur_month': o_cur_month,
'o_per_month': o_per_month,
};
}
factory Order.fromMap(Map<String, dynamic> map) {
return Order(
total_order: map['total_order'],
o_cur_month: map['o_cur_month'],
o_per_month: map['o_per_month'],
);
}
String toJson() => json.encode(toMap());
factory Order.fromJson(String source) => Order.fromMap(json.decode(source));
}
class Customer {
final String total_customer;
final String c_cur_month;
final String c_per_month;
Customer({
required this.total_customer,
required this.c_cur_month,
required this.c_per_month,
});
Customer copyWith({
String? total_customer,
String? c_cur_month,
String? c_per_month,
}) {
return Customer(
total_customer: total_customer ?? this.total_customer,
c_cur_month: c_cur_month ?? this.c_cur_month,
c_per_month: c_per_month ?? this.c_per_month,
);
}
Map<String, dynamic> toMap() {
return {
'total_customer': total_customer,
'c_cur_month': c_cur_month,
'c_per_month': c_per_month,
};
}
factory Customer.fromMap(Map<String, dynamic> map) {
return Customer(
total_customer: map['total_customer'],
c_cur_month: map['c_cur_month'],
c_per_month: map['c_per_month'],
);
}
String toJson() => json.encode(toMap());
factory Customer.fromJson(String source) =>
Customer.fromMap(json.decode(source));
}
class Pending {
final String total_pending_top;
final String total_pending_bottom;
final String cur_month_top;
final String cur_month_bottom;
final String per_month_top;
final String per_month_bottom;
final String six_month_top;
final String six_month_bottom;
Pending({
required this.total_pending_top,
required this.total_pending_bottom,
required this.cur_month_top,
required this.cur_month_bottom,
required this.per_month_top,
required this.per_month_bottom,
required this.six_month_top,
required this.six_month_bottom,
});
Pending copyWith({
String? total_pending_top,
String? total_pending_bottom,
String? cur_month_top,
String? cur_month_bottom,
String? per_month_top,
String? per_month_bottom,
String? six_month_top,
String? six_month_bottom,
}) {
return Pending(
total_pending_top: total_pending_top ?? this.total_pending_top,
total_pending_bottom: total_pending_bottom ?? this.total_pending_bottom,
cur_month_top: cur_month_top ?? this.cur_month_top,
cur_month_bottom: cur_month_bottom ?? this.cur_month_bottom,
per_month_top: per_month_top ?? this.per_month_top,
per_month_bottom: per_month_bottom ?? this.per_month_bottom,
six_month_top: six_month_top ?? this.six_month_top,
six_month_bottom: six_month_bottom ?? this.six_month_bottom,
);
}
Map<String, dynamic> toMap() {
return {
'total_pending_top': total_pending_top,
'total_pending_bottom': total_pending_bottom,
'cur_month_top': cur_month_top,
'cur_month_bottom': cur_month_bottom,
'per_month_top': per_month_top,
'per_month_bottom': per_month_bottom,
'six_month_top': six_month_top,
'six_month_bottom': six_month_bottom,
};
}
factory Pending.fromMap(Map<String, dynamic> map) {
return Pending(
total_pending_top: map['total_pending_top'],
total_pending_bottom: map['total_pending_bottom'],
cur_month_top: map['cur_month_top'],
cur_month_bottom: map['cur_month_bottom'],
per_month_top: map['per_month_top'],
per_month_bottom: map['per_month_bottom'],
six_month_top: map['six_month_top'],
six_month_bottom: map['six_month_bottom'],
);
}
String toJson() => json.encode(toMap());
factory Pending.fromJson(String source) =>
Pending.fromMap(json.decode(source));
}
class Receiving {
final String total_rec_top;
final String total_rec_bottom;
final String cur_month_top;
final String cur_month_bottom;
final String per_month_top;
final String per_month_bottom;
final String average_sales_tops;
final String average_sales_Bottoms;
Receiving({
required this.total_rec_top,
required this.total_rec_bottom,
required this.cur_month_top,
required this.cur_month_bottom,
required this.per_month_top,
required this.per_month_bottom,
required this.average_sales_tops,
required this.average_sales_Bottoms,
});
Receiving copyWith({
String? total_rec_top,
String? total_rec_bottom,
String? cur_month_top,
String? cur_month_bottom,
String? per_month_top,
String? per_month_bottom,
String? average_sales_tops,
String? average_sales_Bottoms,
}) {
return Receiving(
total_rec_top: total_rec_top ?? this.total_rec_top,
total_rec_bottom: total_rec_bottom ?? this.total_rec_bottom,
cur_month_top: cur_month_top ?? this.cur_month_top,
cur_month_bottom: cur_month_bottom ?? this.cur_month_bottom,
per_month_top: per_month_top ?? this.per_month_top,
per_month_bottom: per_month_bottom ?? this.per_month_bottom,
average_sales_tops: average_sales_tops ?? this.average_sales_tops,
average_sales_Bottoms:
average_sales_Bottoms ?? this.average_sales_Bottoms,
);
}
Map<String, dynamic> toMap() {
return {
'total_rec_top': total_rec_top,
'total_rec_bottom': total_rec_bottom,
'cur_month_top': cur_month_top,
'cur_month_bottom': cur_month_bottom,
'per_month_top': per_month_top,
'per_month_bottom': per_month_bottom,
'average_sales_tops': average_sales_tops,
'average_sales_Bottoms': average_sales_Bottoms,
};
}
factory Receiving.fromMap(Map<String, dynamic> map) {
return Receiving(
total_rec_top: map['total_rec_top'],
total_rec_bottom: map['total_rec_bottom'],
cur_month_top: map['cur_month_top'],
cur_month_bottom: map['cur_month_bottom'],
per_month_top: map['per_month_top'],
per_month_bottom: map['per_month_bottom'],
average_sales_tops: map['average_sales_tops'],
average_sales_Bottoms: map['average_sales_Bottoms'],
);
}
String toJson() => json.encode(toMap());
factory Receiving.fromJson(String source) =>
Receiving.fromMap(json.decode(source));
}
这是使用:https://marketplace.visualstudio.com/items?itemName=BendixMa.dart-data-class-generator
然后你可以继续这样做:
Future<DashboardModel?> _getDashboard() async {
try {
var res = await http.post(API.get_dashboard2, body: parameters);
final _dashboard = DashboardModel.fromJson(res.body);
if (_dashboard.success == 1 && res.statusCode == 200) {
return _dashboard;
} else if (_dashboard.success == 0) {
Toast.show(dashboard.message, context,
gravity: Toast.BOTTOM, duration: Toast.LENGTH_SHORT);
}
} catch (e) {
print(e.toString());
return null;
}
}