Introduction
GetX is a powerful micro-framework for Flutter that combines state management, dependency injection, and navigation in one lightweight package. This guide will walk you through installing GetX in your Flutter project and setting up your first GetX-powered app.
Step 1: Add GetX to pubspec.yaml
Open your pubspec.yaml file and add the GetX dependency under dependencies:
After adding, run flutter pub get in your terminal to install the package. You can also add GetX via command line: flutter pub add get.
Step 2: Import GetX
In any Dart file where you want to use GetX, import the package:
Step 3: Replace MaterialApp with GetMaterialApp
To enable all GetX features (navigation, snackbars, dialogs, etc.), replace MaterialApp with GetMaterialApp in your main.dart:
GetMaterialApp is a drop-in replacement for MaterialApp and adds GetX’s routing, internationalization, and other capabilities. It is required for features like Get.to(), Get.snackbar(), and Get.dialog().
Step 4: Create a Controller
A controller in GetX holds your business logic and reactive state. Create a class that extends GetxController:
Step 5: Use the Controller in Your View
In your widget, inject the controller using Get.put() and use Obx to react to changes:
Step 6: Run Your App
Run flutter run and you’ll see a working counter app powered by GetX. Tap the buttons to see the UI update reactively.
Verifying the Installation
To ensure GetX is correctly installed, you can use Get.snackbar anywhere in your app. Add this line to a button’s onPressed:
If the snackbar appears, GetX is properly set up.
Alternative Installation Methods
- Using Flutter CLI: Run
flutter pub add getin your project terminal. - Using Git dependency: For the latest development version, reference the Git repository directly in
pubspec.yaml:dependencies: get: git: url: https://github.com/jonataslaw/getx.git - Specifying a specific version: Replace
^4.6.6with any version you need (e.g.,4.6.5).
Common Issues & Solutions
- Error: 'GetMaterialApp' not found – Make sure you imported
package:get/get.dartand ranflutter pub get. - Build fails with version conflicts – Check your
pubspec.lockfor conflicting dependencies; sometimes upgrading all packages helps. - Hot reload not working properly – A full restart (hot restart) may be needed after adding GetX or making changes to the controller.
- Null safety issues – GetX 4.x fully supports null safety. Ensure your Flutter SDK is >= 2.10.0.
- GetX not recognized in IDE – Try restarting your IDE (VS Code / Android Studio) after
flutter pub get.
Best Practices After Installation
- Use bindings for dependency management – Instead of
Get.putinside widgets, useGet.lazyPutinside bindings. - Organize controllers into modules – Group related controllers and views in feature folders.
- Use
GetViewfor cleaner widgets – Reduces repetitiveGet.findcalls. - Enable
permanent: trueonly for global services – Avoid keeping controllers alive unnecessarily.
Next Steps
Now that GetX is installed and set up, you can explore more advanced features:
Key Takeaways
- Add
get: ^4.6.6to pubspec.yaml and runflutter pub get. - Replace
MaterialAppwithGetMaterialApp. - Import
package:get/get.dart. - Create controllers by extending
GetxControllerand use.obsfor reactive variables. - Inject controllers with
Get.put()and listen to changes withObx(). - Verify installation with
Get.snackbar.