dynamic body = {
'doctor_availablity_done': StatusEnum.Yes.index.toString(),
'emergency_call': emergencyCall,
'weekends': {
"days": selectedWeekendList.toString(),
"time_period": {
"morning": {
"start_time": weekendMorningStart.value.toString(),
"end_time": weekendMorningEnd.value.toString()
},
"afternoon": {
"start_time": weekendAfternoonStart.value.toString(),
"end_time": weekendAfternoonEnd.value.toString()
},
"evening": {
"start_time": weekendEveningStart.value.toString(),
"end_time": weekendEveningEnd.value.toString()
},
"night": {
"start_time": weekendNightStart.value.toString(),
"end_time": weekendNightStart.value.toString()
}
}
}.toString(),
'weekdays': {
"days": selectedWeekdaysList.toString(),
"time_period": {
"morning": {
"start_time": weekdaysMorningStart.value.toString(),
"end_time": weekdaysMorningEnd.value.toString()
},
"afternoon": {
"start_time": weekdaysAfternoonStart.value.toString(),
"end_time": weekdaysAfternoonEnd.value.toString()
},
"evening": {
"start_time": weekdaysEveningStart.value.toString(),
"end_time": weekdaysEveningEnd.value.toString()
},
"night": {
"start_time": weekdaysNightStart.value.toString(),
"end_time": weekdaysNightStart.value.toString()
}
}
}.toString(),
};
I want to pass this whole body content in JSON format. The above data is in string. How do I pass data in JSON. I'm trying to pass it in jsonEncode(body.toString()) but it throws error of invalid data.
CodePudding user response:
You can do this way
var body = {};
var weekends = {};
var timePeriod = {};
body["doctor_availablity_done"] = "1";
body["emergency_call"] = "emergencyCall";
weekends["days"] = "selectedWeekendList.toString()";
timePeriod["morning"] = {"start_time": "ads", "end_time": "qsasds"};
weekends["time_period"] = timePeriod;
body["weekends"] = weekends;
print(jsonEncode(body));
Other keys you can append. this is just sample.
CodePudding user response:
You shouldn't call toString()
on weekends and weekdays, and also not on the result, so like this
dynamic body = {
'doctor_availablity_done': StatusEnum.Yes.index.toString(),
'emergency_call': emergencyCall,
'weekends': {
"days": selectedWeekendList.toString(),
"time_period": {
"morning": {
"start_time": weekendMorningStart.value.toString(),
"end_time": weekendMorningEnd.value.toString()
},
"afternoon": {
"start_time": weekendAfternoonStart.value.toString(),
"end_time": weekendAfternoonEnd.value.toString()
},
"evening": {
"start_time": weekendEveningStart.value.toString(),
"end_time": weekendEveningEnd.value.toString()
},
"night": {
"start_time": weekendNightStart.value.toString(),
"end_time": weekendNightStart.value.toString()
}
}
},
'weekdays': {
"days": selectedWeekdaysList.toString(),
"time_period": {
"morning": {
"start_time": weekdaysMorningStart.value.toString(),
"end_time": weekdaysMorningEnd.value.toString()
},
"afternoon": {
"start_time": weekdaysAfternoonStart.value.toString(),
"end_time": weekdaysAfternoonEnd.value.toString()
},
"evening": {
"start_time": weekdaysEveningStart.value.toString(),
"end_time": weekdaysEveningEnd.value.toString()
},
"night": {
"start_time": weekdaysNightStart.value.toString(),
"end_time": weekdaysNightStart.value.toString()
}
}
},
};
String json = jsonEncode(body);
CodePudding user response:
Object data = {
"username": userName,
"password": password,
};
Uri url = Uri.parse("url");
final response = await http.post(url, body: data);