Answer
Overview
In GetX,
text
Get.put()text
Get.lazyPut()Get.put() — Eager Instantiation
Creates the instance immediately when
text
put()dart// Instance created RIGHT NOW final controller = Get.put(HomeController()); // With tag (for multiple instances of same type) Get.put(ProductController(), tag: 'featured'); Get.put(ProductController(), tag: 'trending'); // permanent = true → never disposed Get.put(AuthService(), permanent: true);
Use when:
- You need the controller immediately on app/screen start
- You want the controller ready before any widget reads it
Get.lazyPut() — Lazy Instantiation
Creates the instance only when first accessed via
text
Get.find()dart// Instance NOT created yet — just registered Get.lazyPut<HomeController>(() => HomeController()); // fenix: true → recreate if disposed and accessed again Get.lazyPut<ApiService>(() => ApiService(), fenix: true); // With tag Get.lazyPut<ProductController>( () => ProductController(), tag: 'featured', ); // First call to Get.find() triggers creation final controller = Get.find<HomeController>(); // ← created here
Use when:
- Inside Bindings (standard practice)
- When controller is optional and may not always be needed
- To improve app startup performance
Comparison Table
| Feature | text | text |
|---|---|---|
| When created | Immediately | On first text |
| Memory usage | Higher (upfront) | Lower (on demand) |
| Startup performance | Slower (creates now) | Faster (deferred) |
| Availability | Ready immediately | Available after first find |
| Syntax | text | text |
| Factory | Direct instance | Function that returns instance |
| fenix option | ❌ No | ✅ Yes (recreate after dispose) |
fenix: true — Recreate After Dispose
dartGet.lazyPut<CartController>(() => CartController(), fenix: true); // User opens cart → CartController created Get.find<CartController>(); // User leaves cart → CartController disposed // User opens cart again → CartController recreated (fenix: true) Get.find<CartController>(); //新 instance created again
Without
text
fenix: trueWhich to Use?
| Scenario | Recommendation |
|---|---|
| App-wide services (auth, API) | text |
| Screen-specific controllers | text |
| Controller needed immediately | text |
| Optional/heavy controllers | text |
| Controller may be disposed and reused | text |
Best Practice: Use
for permanent app-level dependencies andtextGet.put()inside Bindings for screen-level controllers.textGet.lazyPut()