Flutter GetX Masterclass — Chapter 1: Setting Up GetX in Your Flutter Project

Amanullah Bahram
3 min readOct 17, 2024

--

Flutter is a powerful framework for building cross-platform applications, but without effective state management, your app can become challenging to scale. In this article, we’ll dive into Chapter 1 of the GetX Masterclass where we set up GetX, one of the most popular and efficient state management packages for Flutter. We’ll cover everything from installation to creating a simple product listing app.

Why Use GetX?

GetX simplifies state management, routing, and dependency injection. It’s lightweight, and powerful, and allows you to write less code while achieving more functionality. Whether you’re working on a small app or a large enterprise solution, GetX helps you manage complexity efficiently.

📦 Step 1: Install GetX

To begin, you’ll need to add GetX to your Flutter project. In your `pubspec.yaml` file, add the following dependency:

dependencies:
get: ^4.6.5

Run `flutter pub get` to install the package.

🗂 Step 2: Set Up a Scalable Folder Structure

Next, let’s organize our project using a modular folder structure. This helps in separating concerns, making your project easy to maintain as it grows.

Here’s the folder structure we’ll use:

lib/

├── app/
│ ├── modules/
│ │ ├── product/
│ │ │ ├── controllers/
│ │ │ │ └── product_controller.dart
│ │ │ ├── views/
│ │ │ │ └── product_view.dart
│ │ │ │ └── product_details_view.dart
│ │ │ └── models/
│ │ │ └── product_model.dart
│ └── routes/
│ └── app_pages.dart
└── main.dart

This modular structure will allow us to keep our project organized as we add features in later chapters.

🔄 Step 3: Create a Simple Product Listing App

Let’s build a simple product listing app. We’ll create a `ProductController` to manage our products, a `ProductModel` for data representation, and views for listing and viewing product details.

Product Model
Here’s our basic `ProductModel`:

class ProductModel {
final String name;
final double price;
ProductModel({required this.name, required this.price});
}

Product Controller
The `ProductController` will manage our product list. We’ll use GetX’s reactive programming capabilities to observe changes in the product list.


import 'package:get/get.dart';
import '../models/product_model.dart';

class ProductController extends GetxController {
var products = <ProductModel>[].obs;
@override
void onInit() {
super.onInit();
// Initialize the product list
products.addAll([
ProductModel(name: 'Product 1', price: 29.99),
ProductModel(name: 'Product 2', price: 59.99),
]);
}
}

Product View
The `ProductView` will display a list of products, and upon tapping a product, it will navigate to the product details page.


import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/product_controller.dart';

class ProductView extends StatelessWidget {
final ProductController productController = Get.find<ProductController>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Product List')),
body: Obx(() {
return ListView.builder(
itemCount: productController.products.length,
itemBuilder: (context, index) {
final product = productController.products[index];
return ListTile(
title: Text(product.name),
subtitle: Text('\$${product.price}'),
onTap: () => Get.toNamed('/product-details', arguments: product),
);
},
);
}),
);
}
}

🚀 Step 4: Routing with GetX

GetX makes navigation a breeze. In `app_pages.dart`, define your routes like this:


import 'package:get/get.dart';
import '../modules/product/views/product_view.dart';
import '../modules/product/views/product_details_view.dart';
part 'app_routes.dart';

class AppPages {
static final routes = [
GetPage(name: Routes.PRODUCT, page: () => ProductView()),
GetPage(name: Routes.PRODUCT_DETAILS, page: () => ProductDetailsView()),
];
}

🎉 What’s Next?

That’s it for Chapter 1! You’ve set up a scalable folder structure, installed GetX, and created a simple app with routing. In the next chapters, we’ll cover more advanced topics such as state management, dependency injection, and API integration.

🔗 Watch the Full Tutorial on YouTube

Don’t forget to check out the full tutorial on YouTube, where I walk you through the process step by step. You can follow along with the code and even build your app as you learn!

[Watch the Tutorial Here](https://www.youtube.com/watch?v=X7EjiQEmrBI)

Stay tuned for Chapter 2, where we dive into Reactive State Management with GetX. If you have any questions or feedback, feel free to leave a comment or reach out on GitHub!

Happy Coding! 🎉

#fluttertutorial #fluttergetx #getx #getxtutorial

--

--

Amanullah Bahram
Amanullah Bahram

No responses yet