Json that I have posted in body..
{
"products": [
{"id":1, "qty": 1},{"id":2, "qty": 1}
],
"vendor_id": 1,
"notes": " ",
"address": ""
}
Your Post Api call be like:
Future<Order> postOrder(int vendor_id, String notes, String address, int
id1,int q1, int id2, int q2) async {
Paste your api url here
String url = '';
final response = await http.post(apiUrl, headers: {
// Enter your headers parameter if needed
// E.g:
'Authorization' : 'xyz',
'Content-Type' : 'application/json'
},
body: jsonEncode(<String, String>{
'vendor_id' : vendor_id,
'notes' : notes,
'address' : address,
'products' : [
{'id': id1,'qty' : q1},
{'id' : id2,'qty' : q2}
]
}));
if (response.statusCode == 200) {
var data = jsonDecode(response.body.toString());
print(data);
return Order.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to post user.');
}
}
Your model be like:
class Order {
List<Products>? products;
int? vendorId;
String? notes;
String? address;
Order({this.products, this.vendorId, this.notes, this.address});
Order.fromJson(Map<String, dynamic> json) {
if (json['products'] != null) {
products = <Products>[];
json['products'].forEach((v) {
products!.add(new Products.fromJson(v));
});
}
vendorId = json['vendor_id'];
notes = json['notes'];
address = json['address'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.products != null) {
data['products'] = this.products!.map((v) => v.toJson()).toList();
}
data['vendor_id'] = this.vendorId;
data['notes'] = this.notes;
data['address'] = this.address;
return data;
}
}
class Products {
int? id;
int? qty;
Products({this.id, this.qty});
Products.fromJson(Map<String, dynamic> json) {
id = json['id'];
qty = json['qty'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['qty'] = this.qty;
return data;
}
}