API integration in Flutter Using GETX
API integration in Flutter Using GETX
here’s a step-by-step guide to define an API endpoint, make a request, store the received data in
GetX controllers, and update the UI based on the state:
2. Create a Model
Create a Dart model to represent the data. For this example, let’s assume you want to store user
information (name, username, and email).
dart
class UserModel {
return UserModel(
name: json['name'],
username: json['username'],
email: json['email'],
);
import 'dart:convert';
if (response.statusCode == 200) {
} else {
import 'package:get/get.dart';
import 'user_model.dart';
import 'api_service.dart';
try {
} finally {
@override
void onInit() {
super.onInit();
Use Obx to listen to the state changes and update the UI when the data changes.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
@override
return Scaffold(
appBar: AppBar(
title: Text('Users'),
),
body: Obx(() {
// Check if loading
if (userController.isLoading.value) {
return Center(child: CircularProgressIndicator());
} else if (userController.userList.isEmpty) {
} else {
return ListView.builder(
itemCount: userController.userList.length,
return ListTile(
title: Text(user.name),
subtitle: Text(user.username),
trailing: Text(user.email),
);
},
);
}),
);
6. Complete Setup
Step 1: Define the UserModel class for the structure of the data.
Step 2: Create a service (ApiService) to handle the API request and fetch the data.
Step 3: Use a GetX controller (UserController) to manage the state and handle data fetching.
Step 4: Update the UI in UserScreen using Obx to reactively display the fetched data.
Key Points:
GetX Controller (UserController) manages the state of the fetched data and the loading
indicator.
Concept Overview
Imagine you are running a shop, and you want to display a list of items (users) on a board. The
process involves:
1. API (Shop Inventory): Think of the API as the supplier’s catalog. You request a list of available
items (users) from the supplier (API).
2. GetX Controller (Shop Manager): The controller is the shop manager who checks the
inventory (the data from the API) and stores it in the shop (your app).
3. Observable State (Shop Inventory List): The shop manager keeps a list of all items in the
inventory. If the supplier updates the catalog, the list in the shop is automatically updated
(thanks to GetX observables).
4. UI (Display Board): The board (UI) is updated based on the items in the shop's inventory. If
the shop manager (controller) updates the list, the board updates itself without you having
to refresh it.
1. Request Data: The app requests user data from an online server (API). This is like asking for a
list of products from the supplier.
2. Store Data in Controller: Once the app receives the data, it passes the information to the
controller (the shop manager), who stores it and takes care of it.
3. Update UI Automatically: The controller tells the display board (UI) what to show based on
the data it holds. If the data changes, the board updates automatically without any extra
effort.
Flowchart
Below is a flow chart that describes this entire process step by step:
plaintext
Copy code
┌─────────────────────────────────────────────────┐
│ Start App │
└─────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────┐
┌─────────────────────────────────────────────────┐
└─────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────┐
└─────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────┐
└─────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────┐
└─────────────────────────────────────────────────┘
Explanation of Flow:
2. Make Request to API: The app makes a request to the API to get the user data (like asking
the supplier for product details).
3. API Sends Data: The API responds with the requested data.
4. Controller Receives Data: The controller receives the data and stores it (like the shop
manager receiving and organizing products).
5. Update UI: The app’s UI (display board) automatically updates based on the data that the
controller has.
6. Users Can See Data: The user sees the updated data on the app without refreshing it.
In this flow, GetX acts like the "manager" that handles the data, while the API provides the data, and
the UI updates itself whenever the data changes!
4o