Get Connect is the easiest way to communicate from your backend to front end with https and sockets. You can simply extract Get Connect from Getx Package and use the API methods such as, GET/POST/PUT/DELETE/SOCKET to connect with your APIs.
Why using Get connect
Coming our mind, why we need to use Get Connect, instead of having http package or Dio package, what it is overcoming the existing package, let’s see, using Get Connect to call the APIs we don’t need the additional packages to install it.
flutter:
sdk: flutter
get: ^4.6.6
Install GetX package, and you can achieve lot of things from the one package, you can achieve routing and navigation, using GetX as state management, and also using for calling api request.
Create class as Dependency injection where we will initialize our GetConnect class.
class DependencyInjection {
static void init() async {
Get.put<GetConnect>(GetConnect());
Get.put<RestAPI>(RestAPI()); //initializing GetConnect
}
}
Next we need to create rest Api common class which is for calling different methods such as Get,Post,Put,and Delete. Let’s see the coulple of methods for API requests.
//GET request example
Future<dynamic> getDataMethod(String url) async {
Response response = await connect.get(url);
if(response.statusCode == 200) {
print("Response body : ${response.body}");
return response.body;
}else{
return null;
}
}
//post request example
Future<dynamic> postDataMethod(String url,Map<String, String> formData) async {
Response response = await connect.post(url, formData,contentType: 'application/json');
print("status code : ${response.statusCode}");
if(response.statusCode == 200) {
return response.bodyString;
}else{
return response.bodyString;
}
}
We can use Get view widget in UI Part to bind you data which is comes from API response. after that creating GetxController userd for whenever you want to fetch and retrieve an instance of the controller within the widget itself.
Advantages of using Getx
- Get Arguments – This is for retrieve the current screen data which is comes from previously routed device.
- Check platform using Getx using this lines of code. such as
- GetPlatform.isAndroid
- GetPlatform.isIOS
- GetPlatform.isMacOS
- GetPlatform.isWindows
- GetPlatform.isLinux
- GetPlatform.isFuchsia
- Getting height and width, If you need a changeable height/width (like Desktop or browser windows that can be scaled) you will need to use context.
GetxService
It creates and manages the dependencies through the Flutter app, once it is initialized. For example, listen your internet connection throughout your app. Create a class using getxservice,
class NetworkStatusService extends GetxService {
NetworkStatusService() {
Connectivity().onConnectivityChanged.listen(
(status) async {
_getNetworkStatus(status);
}, );
}
void _getNetworkStatus(ConnectivityResult status) {
if (status == ConnectivityResult.mobile || status == ConnectivityResult.wifi) {
print("Internet connected");
} else {
print("Lost the connection");
}
}
}
Initialize your service class by adding to your main.dart file with Get.put().
Future<void> main() async {
Get.put < NetworkStatusService > (NetworkStatusService(), permanent: true);
}
Summary
Using Getx features it will getting fast development and optimized your code comparing than other packages such as BLOC, Provider and etc. Simplify your API requests using GetConnect without using another packages such as Dio and HTTP requests .The most incredible fact about Getx Service is that will automatically manages the lifecycle of the service, which means you need initialize and dispose of objects which is created.
No Comment! Be the first one.