Issue
I have this json format that i need to pass to api,it is a nested json and i am struggling with pass this to api,I have tried following way but it always throws format error
Json format
{
"id":116,
"station_id":2,
"branch_id":87,
"manager_id":null,
"employee_id":null,
"vehicle_id":24,
"customer_name":"SDSADSA",
"mobile":"3213213213",
"location":"SDSAD",
"vehicle_model":null,
"register_number":"ASDASD",
"service_types":[
1
],
"accessories":[
],
"suggestion":null,
"inspection_comment":"SDSAD",
"amount":"100.00",
"extra_amount":"0.00",
"discount":"0.00",
"total_amount":"100.00",
"feedback":null,
"working_status_id":2,
"remarks":null,
"old_stock_details":[
{
"station_id":2,
"branch_id":87,
"branch_stock":{
"id":14,
"stock_id":4,
"station_id":2,
"branch_id":87,
"kg":4,
"gm":200,
"ltr":0,
"ml":0,
"counter":0,
"stock":{
"id":4,
"station_id":2,
"name":"Wash Powder",
"unit":1,
"stock_date":"2021-12-10"
}
},
"stock_id":4,
"unit":1,
"kg":1,
"gm":100,
"ltr":0,
"ml":0,
"counter":0
}
],
"new_stock_details":[
{
"station_id":2,
"branch_id":87,
"branch_stock":{
"id":14,
"stock_id":4,
"station_id":2,
"branch_id":87,
"kg":4,
"gm":200,
"ltr":0,
"ml":0,
"counter":0,
"stock":{
"id":4,
"station_id":2,
"name":"Wash Powder",
"unit":1,
"stock_date":"2021-12-10"
}
},
"stock_id":4,
"unit":1,
"kg":1,
"gm":100,
"ltr":0,
"ml":0,
"counter":0
}
]
}
This is what i did it so far
empData.then((data) async {
var empId = data;
stationData.then((data) async {
var stationId = data;
tokenData.then((datas) async {
Branchdata.then((br_data) async {
var token = datas;
var branch_id=br_data;
var data = {};
var new_stock_details = [];
var old_stock_details = [];
var stocks = {};
data["id"] = id;
data["station_id"] = stationId;
data["employee_id"] = selectedemp;
data["branch_id"] = branch_id;
data["manager_id"] = empId;
data["customer_name"] = name;
data["mobile"] = mobile;
data["location"] = location;
data["vehicle_id"] = vehicleId;
data["register_number"] = registerNumber;
data["service_types"] = serviceTypeId;
data["accessories"] = accessoryId;
data["working_status_id"] = workingStatusId;
data["suggestion"] = suggesionbox;
data["amount"] = amount;
data["extra_amount"] = extraAmount;
data["feedback"] = feedback;
data["discount"] = discount;
data["remarks"] = cancelRemark;
data["role_id"] = roleId;
for (int i = 0; i < list_add_old_stock_details.length; i++) {
stocks = {};
stocks["stock_id"] = list_add_old_stock_details[i].stock_id;
stocks["branch_id"] = list_add_old_stock_details[i].branch_id;
stocks["station_id"] = list_add_old_stock_details[i].station_id;
stocks["kg"] = list_add_old_stock_details[i].kg;
stocks["gm"] = list_add_old_stock_details[i].mg;
stocks["ltr"] = list_add_old_stock_details[i].litre;
stocks["ml"] =list_add_old_stock_details[i].mlil;
stocks["counter"] = list_add_old_stock_details[i].count;
new_stock_details.add(stocks);
}
data["old_stock_details"] = old_stock_details;
for (int i = 0; i < list_add_stock_details.length; i++) {
stocks = {};
stocks["stock_id"] = list_add_stock_details[i].stock_id;
stocks["branch_id"] = list_add_stock_details[i].branch_id;
stocks["station_id"] = list_add_stock_details[i].station_id;
stocks["kg"] = list_add_stock_details[i].kg;
stocks["gm"] = list_add_stock_details[i].mg;
stocks["ltr"] = list_add_stock_details[i].litre;
stocks["ml"] = list_add_stock_details[i].mlil;
stocks["counter"] = list_add_stock_details[i].count;
new_stock_details.add(stocks);
}
data["new_stock_details"] = new_stock_details;
jsonEncode(data);
var response = await http.post(Uri.parse(Urls.VEHICLE_REGISTER),
headers: {
"Content-Type": "application/json",
'Authorization': 'Bearer $token'
},
body: jsonEncode(data));
print('Response status: ${response.statusCode}');
print('Response dashboard body: ${response.body}');
Map<String, dynamic> value = json.decode(response.body);
var success = value['success'];
var message = value['message'];
EasyLoading.dismiss();
if (response.statusCode == 200) {
if (success == true) {
EasyLoading.dismiss();
nameController.clear();
mobileController.clear();
locationController.clear();
registerController.clear();
CoolAlert.show(
context: context,
type: CoolAlertType.success,
text: message.toString(),
onConfirmBtnTap: () {
Navigator.of(context, rootNavigator: true).pop(true);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ListScreen(listType),
),
);
},
);
} else {
EasyLoading.dismiss();
final snackBar = SnackBar(content: Text(message));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
} else {
EasyLoading.dismiss();
final snackBar = SnackBar(content: Text(message));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
});
});
});
});
Solution
Finally figure it out thanks to @Elvis Salabarria Aqunio the model is very helpful,list_stock_details is incremented by add new button and i have made model for required
empData.then((data) async {
var empId = data;
stationData.then((data) async {
var stationId = data;
tokenData.then((datas) async {
Branchdata.then((br_data) async {
var token = datas;
var branch_id = br_data;
EasyLoading.dismiss();
List<NewStockDetails> listModel = [];
List<OldStockDetails> old_listModel = [];
for (int i = 0; i < list_stock_details.length; i++) {
print(list_stock_details[i].unit);
listModel.add(NewStockDetails(
stationId: int.parse(stationId),
branchId: int.parse(branch_id),
stockId: int.parse(list_stock_details[i].stock_id),
branchStock: BranchStock1(
id: int.parse(list_stock_details[i].id),
kg: int.parse(list_stock_details[i].kg),
gm: int.parse(list_stock_details[i].gm),
counter: int.parse(list_stock_details[i].count),
ml: int.parse(list_stock_details[i].mlil),
ltr: int.parse(list_stock_details[i].litre),
stock: Stock1(
id: int.parse(list_stock_details[i].id),
stationId: int.parse(stationId),
name: list_stock_details[i].stockname,
unit: list_stock_details[i].unit,
stockDate: "")),
unit: list_stock_details[i].unit,
kg: int.parse(list_stock_details[i].kg),
gm: int.parse(list_stock_details[i].gm),
counter: int.parse(list_stock_details[i].count),
ml: int.parse(list_stock_details[i].mlil),
ltr: int.parse(list_stock_details[i].litre),
));
}
AddStock addJson = new AddStock(
id: int.parse(id),
stationId: int.parse(stationId),
branchId: int.parse(branch_id),
vehicleId: int.parse(vehicleId),
customerName: name,
mobile: mobile,
location: location,
registerNumber: registerNumber,
serviceTypes: serviceTypeId,
accessories: accessoryId,
suggestion: suggesionbox,
amount: amount,
extraAmount: extraAmount,
feedback: feedback,
discount: discount,
totalAmount: totalAmount,
remarks: cancelRemark,
inspectionComment: "test",
workingStatusId: int.parse(workingStatusId),
newStockDetails: listModel,
oldStockDetails: old_listModel,
);
var response = await
http.post(Uri.parse(Urls.VEHICLE_REGISTER),
headers: {
"Content-Type": "application/json",
'Authorization': 'Bearer $token'
},
body: jsonEncode(addJson));
print('Response status: ${response.statusCode}');
print('Response dashboard body: ${response.body}');
Map<String, dynamic> value = json.decode(response.body);
var success = value['success'];
var message = value['message'];
EasyLoading.dismiss();
if (response.statusCode == 200) {
if (success == true) {
EasyLoading.dismiss();
nameController.clear();
mobileController.clear();
locationController.clear();
registerController.clear();
CoolAlert.show(
context: context,
type: CoolAlertType.success,
text: message.toString(),
onConfirmBtnTap: () {
Navigator.of(context, rootNavigator: true).pop(true);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ListScreen(listType),
),
);
},
);
} else {
EasyLoading.dismiss();
final snackBar = SnackBar(content: Text(message));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
} else {
EasyLoading.dismiss();
final snackBar = SnackBar(content: Text(message));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
});
});
});
});
Answered By – Abhijith
Answer Checked By – Senaida (FlutterFixes Volunteer)