Question #15MediumState Management

Diff in Getx put vs lazy put

#getx

Answer

Overview

In GetX,

text
Get.put()
and
text
Get.lazyPut()
both register dependencies, but differ in when the instance is created.


Get.put() — Eager Instantiation

Creates the instance immediately when

text
put()
is called.

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
Get.put()
text
Get.lazyPut()
When createdImmediatelyOn first
text
Get.find()
Memory usageHigher (upfront)Lower (on demand)
Startup performanceSlower (creates now)Faster (deferred)
AvailabilityReady immediatelyAvailable after first find
Syntax
text
Get.put(Controller())
text
Get.lazyPut(() => Controller())
FactoryDirect instanceFunction that returns instance
fenix option❌ No✅ Yes (recreate after dispose)

fenix: true — Recreate After Dispose

dart
Get.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: true
, accessing a disposed controller throws an error.


Which to Use?

ScenarioRecommendation
App-wide services (auth, API)
text
Get.put(permanent: true)
Screen-specific controllers
text
Get.lazyPut()
in Bindings
Controller needed immediately
text
Get.put()
Optional/heavy controllers
text
Get.lazyPut()
Controller may be disposed and reused
text
Get.lazyPut(fenix: true)

Best Practice: Use

text
Get.put()
for permanent app-level dependencies and
text
Get.lazyPut()
inside Bindings for screen-level controllers.