I created a form when user will put their revenue and the system will calculate the total revenue and store all the data in the database.
When I try submit the inputted data, I received code 400 (Exception: Bad request. Please check the submitted data.)
I want to pass the data using parameters. What is the error in this code?
setState(() {
String OccRoomText = _occRoomController.text;
String ADRText = _adrController.text;
String FnBRevenueText = _fnbrevenueController.text;
String extraRevenueText = _extrarevenueController.text;
if (_formKey.currentState!.validate()) {
print('2');
String hotel = 'DTH';
String date = _dateController.text;
int totalroom = 299;
double OccupiedRoom = double.parse(OccRoomText);
double ADR = double.parse(ADRText);
double FnBRevenue = double.parse(FnBRevenueText);
double ExtraRevenue = double.parse(extraRevenueText);
occRate = double.parse(((OccupiedRoom/totalroom)*100).toStringAsFixed(2));
TotalRoomRevenue = double.parse((OccupiedRoom*ADR).toStringAsFixed(2));
roomRevenue = formatter.format(TotalRoomRevenue.round());
RevPAR = double.parse((TotalRoomRevenue/totalroom).toStringAsFixed(2));
TotalFnBRevenue = double.parse((FnBRevenue + ExtraRevenue).toString());
int FNBRevenue = TotalFnBRevenue.round();
TotalRevenue = formatter.format((TotalRoomRevenue + FNBRevenue).round());
print(TotalRevenue);
Future<http.Response> submitform() async {
print('ready');
final response = await http.post(
Uri.parse('http://xxxx/xxxx/api/hotel/InsertHotelDailyRevenue?hotel=$hotel&RptDate=$date&TotalRooms=<span class="math-inline">totalroom&OccupiedRooms=</span>{OccupiedRoom.toStringAsFixed(2)}&OccupancyRate=$occRate&ADR=</span>{ADR.toStringAsFixed(2)}&RevPAR=$RevPAR&TotalRoomRevenue=$roomRevenue&TotalFnBRevenue=<span class="math-inline">FNBRevenue&TotalRevenue=$TotalRevenue'), // Use toStringAsFixed for double values
headers: <String, String>{
'Content-Type': 'application/x-www-form-urlencoded', // Adjust if needed
},
);
print('after api');
if (response.statusCode == 201) {
print('201');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Data submitted successfully!')),
);
} else {
String errorMessage = 'Failed to submit data.';
switch (response.statusCode) {
case 400:
errorMessage = 'Bad request. Please check the submitted data.';
break;
case 401:
errorMessage = 'Unauthorized. Please check your credentials.';
break;
case 403:
errorMessage = 'Forbidden. You are not authorized to submit data.';
break;
default:
errorMessage = 'Error: ${response.statusCode}';
}
throw Exception(errorMessage);
}
return response;
}
Future<http.Response> response = submitform();
response.then((value) => {
// Handle the response here (e.g., check status code, parse data)
print(value.statusCode)// Print the status code
}).catchError((error) => {
// Handle errors here
print(error)
});
New contributor
unknown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.