I have the below modeling for the JSON data, in which there are orders and items in each order.
DATA MODEL
class NewOrder {
String _error;
List<Content> _content;
NewOrder({String error, List<Content> content}) {
this._error = error;
this._content = content;
}
String get error => _error;
set error(String error) => _error = error;
List<Content> get content => _content;
set content(List<Content> content) => _content = content;
NewOrder.fromJson(Map<String, dynamic> json) {
_error = json['error'];
if (json['content'] != null) {
_content = new List<Content>();
json['content'].forEach((v) {
_content.add(new Content.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['error'] = this._error;
if (this._content != null) {
data['content'] = this._content.map((v) => v.toJson()).toList();
}
return data;
}
}
class Content {
String _orderNo;
List<OrderItems> _orderItems;
Content(
{
String orderNo,
List<OrderItems> orderItems}) {
this._orderNo = orderNo;
this._orderItems = orderItems;
}
set orderNo(String orderNo) => _orderNo = orderNo;
List<OrderItems> get orderItems => _orderItems;
set orderItems(List<OrderItems> orderItems) => _orderItems = orderItems;
Content.fromJson(Map<String, dynamic> json) {
_orderNo = json['order_no'];
if (json['order_items'] != null) {
_orderItems = new List<OrderItems>();
json['order_items'].forEach((v) {
_orderItems.add(new OrderItems.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['order_no'] = this._orderNo;
if (this._orderItems != null) {
data['order_items'] = this._orderItems.map((v) => v.toJson()).toList();
}
return data;
}
}
class OrderItems {
String _compCode;
OrderItems({String compCode, String compName, String orderNo}) {
this._compCode = compCode;
}
String get compCode => _compCode;
set compCode(String compCode) => _compCode = compCode;
OrderItems.fromJson(Map<String, dynamic> json) {
_compCode = json['comp_code'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['comp_code'] = this._compCode;
return data;
}
}
JSON DATA
{
"error": "false",
"content": [
{
"order_no": "16",
"order_items": [
{
"comp_code": "4",
},
{
"comp_code": “5”,
}
]
},
{
"order_no": "18",
"order_items": [
{
"comp_code": “9”,
},
{
"comp_code": “11”,
},
{
"comp_code": “7”,
},
]
}
]
}
I need to display the data in PageView.builder(Horizontal Scroll) and nested ListView.builder(Vertical Scroll), this model I have already created.
I used the below code for associating the JSON Date to the above-configured models, and this function is successfully working
Future<NewOrder> getdetailsoforders(String userid, String companycode) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
Map data = {
'user_id': userid,
'company_code':companycode
};
var response = await http.post(newapi, body: data);
if(response.statusCode == 200) {
jsonResponse = json.decode(response.body);
return NewOrder.fromJson(jsonResponse);
}
}
Now I need to associate the JSON data fields to the Widget model for which I am using the below widget
FutureBuilder(
future: _future,
builder: (context, AsyncSnapshot<Payload> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('none');
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
case ConnectionState.active:
return Text('');
case ConnectionState.done:
if (snapshot.hasError) {
return Text(
'${snapshot.error}',
style: TextStyle(color: Colors.red),
);
} else {
return PageView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.content.length,// length of total orders
itemBuilder: (context, index) {
return Column(
children:<Widget>[
Text('Order Number to be Displayed here'),
ListView.builder(
shrinkWrap: true,
itemCount: //lenght of the items in the order to be determined,
itemBuilder: (context, index) {
return Column(
children: [
Text('Item Name'),
Text('Item description here')
],
);
},
),
])
});
}
}
})
For this final listing of the orders and it's respective items, i need to get the fields from the model, but I am unable to do so, please guide me how can i get those details.
I need the Order NO.
from the content model, then in the same model I need to get the lenght of the order_items
and then the field values of those order items.
I am new to the learning of JSON Flutter, that's why I am facing this issue, please guide me with the solution.
I know I have written a lot of code, but it is to provide a clear picture, what exactly needs to be done.
Replace
builder: (context, AsyncSnapshot<Payload> snapshot)
With
builder: (context, snapshot)
Access data Like:
Text(snapshot.data.content[index].orderNo)