What is GetxService?
GetxService is a specialized class in GetX designed for app-wide services that live throughout the entire application lifecycle. Unlike GetxController, which is typically tied to a route and disposed when the route is closed, GetxService is never automatically disposed (unless manually deleted). This makes it perfect for shared resources like API clients, authentication managers, theme managers, database helpers, and any other singleton that should persist for the app's duration.
GetxService vs GetxController
Creating a Service
To create a service, extend GetxService instead of GetxController. You can add any methods and reactive state just like a controller.
Registering a Service
Register a service using Get.put or Get.lazyPut, usually with permanent: true (though it's automatically permanent). You can also use Get.putAsync for async initialization.
Initialization with onInit and onReady
Services have the same lifecycle methods as controllers: onInit (called after instance creation) and onReady (called after the widget tree is ready). Use them for initializing data, setting up streams, or loading preferences.
Accessing Services Anywhere
Once registered, you can access the service using Get.find<MyService>() from any controller, view, or utility class.
Common Service Examples
Best Practices
- Use services for app-wide logic – API clients, auth, analytics, notifications.
- Keep services stateless when possible – Prefer storing state in controllers; services should provide methods and data access.
- Inject services via constructor – Makes testing easier:
class MyController extends GetxController { final ApiService api; MyController(this.api); }. - Initialize async services in
onInit– Useawaitthere, and considerGet.putAsyncfor registration. - Dispose resources in
onClose– Close database connections, cancel timers, etc. - Use
Get.findinside controllers – Don't re‑register services.
Common Mistakes
- ❌ Using
GetxServicefor screen-specific logic – It will stay in memory forever. ✅ UseGetxControllerfor per‑screen logic. - ❌ Forgetting to dispose resources – Even though services aren't disposed automatically, you should clean up in
onCloseif needed. - ❌ Registering the same service multiple times – Causes duplicates unless you use tags.
✅ Check
Get.isRegisteredbefore registering. - ❌ Assuming
Get.putfor service is lazy – It's not; useGet.lazyPutfor deferred creation.
FAQ
- Q: Do I need to mark a service as
permanent?
A: No,GetxServiceis automatically permanent. It will never be disposed unless you callGet.delete. - Q: Can I use reactive state in a service?
A: Yes, services can have.obsvariables and controllers can listen to them usingObxor workers. - Q: How do I test a service?
A: You can instantiate it directly in tests. For integration, useGet.reset()to clear registrations. - Q: Can a service depend on another service?
A: Yes, simplyGet.findthe dependent service inside the constructor oronInit. - Q: Should I use
Get.putorGet.lazyPutfor services?
A: If the service is needed immediately (e.g., auth check on start), useGet.putwith async init. Otherwise, lazy is fine. - Q: Can I use
GetxServicewith bindings?
A: Yes, you can register services in initialBinding or in per‑route bindings (though they will live beyond the route).
Conclusion
GetxService is a powerful tool for managing global, long‑lived dependencies in your Flutter app. By extending GetxService, you can create clean, testable services that stay alive throughout the app lifecycle. Combine them with GetX dependency injection and reactive state to build robust applications.