Overview of the App
The app performs the following operations:
- Fetch Users (Read): Retrieve a list of users from an API and display them in a scrollable list.
- Create User: Add a new user through a form and post it to the API.
- Side Menu Navigation: A user-friendly side menu provides quick access to key app features.
We'll use JSONPlaceholder as the API for demonstration purposes.
Tech Stack
- Flutter: Framework for cross-platform app development.
- GetX: For state management, navigation, and dependency injection.
- HTTP: For API communication.
- JSONPlaceholder API: Mock API for testing.
Folder Structure
A clear folder structure ensures maintainability:
Code Connections
1. API Integration (api_service.dart)
The ApiService class handles HTTP requests. It includes methods to fetch and create users.
How it connects:
- The
fetchUsers method retrieves a list of users from the API. - The
createUser method sends new user data to the API. - These methods are called in the
UserController to update the app's state.
2. User State Management (user_controller.dart)
The UserController connects the API with the UI. It uses GetX observables to manage state.
How it connects:
- The
fetchUsers method populates the users observable, updating the UI dynamically. - The
createUser method adds a new user to the users list upon success. - Snackbar notifications provide instant feedback.
3. UI Components
Home Screen (home_view.dart): Displays the list of users and includes a floating action button for adding users.
Create User Screen (post_user_view.dart): A form for creating new users.
4. Navigation (app_routes.dart)
Centralized route management simplifies navigation.
How it connects:
- Routes are defined once and used consistently across the app.
How It All Connects
- UserController:
- Fetches data from
ApiService and updates the users observable. - Adds new users to the list when
createUser is called.
- API Service:
- Handles all HTTP operations, isolating networking logic.
- Views:
- Dynamically update based on changes to
users in the controller. - Use GetX's
Obx for reactive state updates.
- Navigation:
- Simplifies screen transitions using GetX routes.
Conclusion
This CRUD app demonstrates how to use Flutter and GetX to build a scalable, responsive application. The modular approach separates concerns, making the app easy to maintain and extend.
What features would you add to improve this app? Let me know in the comments! 🚀