这只是解析您提供的 json 文件的基本示例。
下面是你的 json 文件:
{
"status": 1,
"message": "Data Received successful",
"data": [
{
"billing_address": "BAGHPAT , BAGHPAT , UTTAR PRADESH - 250609",
"delivery_address": "",
"order_input": "0",
"order_no": "Packaging BAWANA121020200001",
"status": "Pending",
"order_date": "12-10-2020",
"order_time": "08:13:53",
"total_quantity": 5,
"gst": "09AUIOP",
"shipp_address": null,
"shipp_city": null,
"shipp_state": null,
"shipp_pincode": null,
"shipp_mobile": null,
"shipp_name": null,
"bill_date": null,
"challan": "",
"bill": "",
"expexted_deliverd_date": "19-10-2020"
}
]
}
以下是我为您提供的 json 创建的模型。
// To parse this JSON data, do
//
// final model = modelFromJson(jsonString);
import 'dart:convert';
Model modelFromJson(String str) => Model.fromJson(json.decode(str));
String modelToJson(Model data) => json.encode(data.toJson());
class Model {
Model({
this.status,
this.message,
this.data,
});
int status;
String message;
List<Datum> data;
factory Model.fromJson(Map<String, dynamic> json) => Model(
status: json["status"],
message: json["message"],
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"status": status,
"message": message,
"data": List<dynamic>.from(data.map((x) => x.toJson())),
};
}
class Datum {
Datum({
this.billingAddress,
this.deliveryAddress,
this.orderInput,
this.orderNo,
this.status,
this.orderDate,
this.orderTime,
this.totalQuantity,
this.gst,
this.shippAddress,
this.shippCity,
this.shippState,
this.shippPincode,
this.shippMobile,
this.shippName,
this.billDate,
this.challan,
this.bill,
this.expextedDeliverdDate,
});
String billingAddress;
String deliveryAddress;
String orderInput;
String orderNo;
String status;
String orderDate;
String orderTime;
int totalQuantity;
String gst;
dynamic shippAddress;
dynamic shippCity;
dynamic shippState;
dynamic shippPincode;
dynamic shippMobile;
dynamic shippName;
dynamic billDate;
String challan;
String bill;
String expextedDeliverdDate;
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
billingAddress: json["billing_address"],
deliveryAddress: json["delivery_address"],
orderInput: json["order_input"],
orderNo: json["order_no"],
status: json["status"],
orderDate: json["order_date"],
orderTime: json["order_time"],
totalQuantity: json["total_quantity"],
gst: json["gst"],
shippAddress: json["shipp_address"],
shippCity: json["shipp_city"],
shippState: json["shipp_state"],
shippPincode: json["shipp_pincode"],
shippMobile: json["shipp_mobile"],
shippName: json["shipp_name"],
billDate: json["bill_date"],
challan: json["challan"],
bill: json["bill"],
expextedDeliverdDate: json["expexted_deliverd_date"],
);
Map<String, dynamic> toJson() => {
"billing_address": billingAddress,
"delivery_address": deliveryAddress,
"order_input": orderInput,
"order_no": orderNo,
"status": status,
"order_date": orderDate,
"order_time": orderTime,
"total_quantity": totalQuantity,
"gst": gst,
"shipp_address": shippAddress,
"shipp_city": shippCity,
"shipp_state": shippState,
"shipp_pincode": shippPincode,
"shipp_mobile": shippMobile,
"shipp_name": shippName,
"bill_date": billDate,
"challan": challan,
"bill": bill,
"expexted_deliverd_date": expextedDeliverdDate,
};
}
根据生成的模型,我已将 json 数据集成到 ui 中:
import 'package:flutter/material.dart';
import 'package:json_parsing_example/model2.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var _isLoading = false;
List<Datum> usersList = [];
@override
void initState() {
super.initState();
getData();
}
getData() async {
setState(() {
_isLoading = true;
});
String data =
await DefaultAssetBundle.of(context).loadString("json/parse.json");
final model = modelFromJson(data);
usersList = model.data;
setState(() {
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Users List "),
),
body: _isLoading
? Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
itemCount: usersList.length,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
var item = usersList[index];
return Card(
child: Padding(
padding: EdgeInsets.all(5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(item.billingAddress),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(item.orderNo),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(item.expextedDeliverdDate),
),
],
),
),
);
}),
));
}
}
这是我提供的示例示例,您可以使用带有提供程序的 MVVM 或使用 bloc 的复杂方式,它在您身上。
我已经提到了一些文章,然后检查一下。
https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51
https://www.youtube.com/watch?v=wc8ecFSu5N8