Here you will learn when and how to use Flutter Getx GetConnect.
If your controllers need to pass base url and they also need to use the service the like get() method or other related method like Post(), Update() and Delete(), then you must extend GetConnect.
So using Getx GetConnect(), you dont need to use http.get() or other related methods from http liabrary.
If you are calling Api request from your app, then GetConnect is the best solution for your controller.
Here is an example, using a controller. I have created a controller name ApiClient. And this controller extends GetConnect and implements GetxService.
Below is the code snippet of GetConnect
class ApiClient extends GetConnect implements GetxService{
late String token;
final String appBaseUrl;
final SharedPreferences sharedPreferences;
late Map<String, String> _mainHeaders;
ApiClient({required this.sharedPreferences, required this.appBaseUrl}) {
baseUrl = appBaseUrl;
// timeout = Duration(seconds: 5);
token = sharedPreferences.getString(AppConstants.TOKEN)??"";
_mainHeaders = {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer $token',
};
}
void updateHeader(String token) {
_mainHeaders = {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer $token',
};
}
Future<Response> getData(String uri,
{Map<String, dynamic>? query, String? contentType,
Map<String, String>? headers, Function(dynamic)? decoder,
}) async {
try {
Response response = await get(
uri,
contentType: contentType,
query: query,
headers: {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer $token', //carrier
},
decoder: decoder,
);
response = handleResponse(response);
return response;
} catch (e) {
return Response(statusCode: 1, statusText: e.toString());
}
}
Future<Response> postData(String uri, dynamic body,) async {
try {
Response response = await post(
uri, body,
// query: query,
// contentType: contentType,
headers: _mainHeaders,
);
response = handleResponse(response);
if(Foundation.kDebugMode) {
log('====> GetX Response: [${response.statusCode}] $uri\n${response.body}');
}
return response;
}catch (e) {
return Response(statusCode: 1, statusText: e.toString());
}
}
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) {
_response = Response(statusCode: 0, statusText: 'Connection to API server failed due to internet connection');
}
return _response;
}
}
From the code if you remove GetConnect, you will get error. Because here we are using Get() and Post() method to make api call.
For the detail code, watch the video below