Here we will learn how to handle api response in Getx for http request. In a nicely structured app project, handling api request is the most important since, it let’s you know the response object, error type, json object, status code like that.
At the end it’s more about how you return json response from the server and how you handle them in flutter.
If we know our response type and error before the client knows, we would be able to show more user friendly message to the client.
Regardless you have http get or post request you can handle them in your specific way. If you use getx state management system it become easier to use since it supports http methods.
In flutter getx you use Response response object to save http request response. Then it becomes easier to know about the specific response details.
Whatever you get from http get or post request you may save them in Getx Response response object.
And then you can analyse the object.
Whatever the response you get from server side send it to this handleResponse() method and you are good to go.
Response handleResponse(Response response) {
Response _response = response;
if(_response.hasError && _response.body != null && _response.body is !String) {
if(_response.body.toString().startsWith('{errors: [{code:')) {
_response = Response(statusCode: _response.statusCode, body: _response.body, statusText: "Error");
}else if(_response.body.toString().startsWith('{message')) {
_response = Response(statusCode: _response.statusCode,
body: _response.body,
statusText: _response.body['message']);
}
}else if(_response.hasError && _response.body == null) {
print("The status code is "+_response.statusCode.toString());
_response = Response(statusCode: 0, statusText: 'Connection to API server failed due to internet connection');
}
return _response;
}
The above function will return a Response object.
If it’s get() request then it will return data info based on server response, and if it’s like post() response then it will also return data.
In general it will return data in certain format
- status code
- body
- status text
It’s very important that, you seperate response body and status code when you return data from server side.
Response body should have correct data or correct error information.
Let’s take a look a bit of server side code.
public function get_order_details(Request $request)
{
$validator = Validator::make($request->all(), [
'order_id' => 'required'
]);
if ($validator->fails()) {
return response()->json(['errors' => Helpers::error_processor($validator)], 403);
}
$dm = DeliveryMan::where(['auth_token' => $request['token']])->first();
$order = Order::with(['details'])->where(['delivery_man_id' => $dm['id'], 'id' => $request['order_id']])->Notpos()->first();
if(!$order)
{
return response()->json([
'errors' => [
['code' => 'order', 'message' => trans('messages.not_found')]
]
], 404);
}
$details = Helpers::order_details_data_formatting($order->details);
return response()->json($details, 200);
}
See the json response format type in the backend. Based on different condition, it returns different json response.
In the below app, we used the same method