2

I am using Getx and its dependency injection mechanism.

sometime I am overthinking - should I inject a class that should remain in memory (for good as a Singelton) using

Get.put(SomeClass(), permanent: true)

or using

Get.put(SomeGetXService())

by reading the documentation, both ways seems to put the class in memory as Singelton, and it can only be deleted explicitly (i.e. not with Get.smartManagement).

as for me, I prefer not to extend the class with GetxService, since the first option is simpler to implement - but I feel like I might be missing something. having the class in memory as Singelton through out the app life-span is a must. Thanks for your help

vigdora
  • 319
  • 4
  • 11

1 Answers1

1

The first option always keeps the class in memory throughout the entire app's lifespan, while the second option binds the service to the stack navigation lifecycle. According to GetX dpcumentation, a GetXService is only deleted with a call to Get.reset(). In most cases though, both methods will probably be equally valid, since in essence, a service is kept in memory throughout the entire app lifecycle anyways.

MrMikimn
  • 721
  • 1
  • 5
  • 19
  • so basically Get.put(SomeGetxController(), permanent: true) and Get.put(SomeGetXService()) are quite equal since both are binded to the navigation lifecycle. the only difference is the way to delete them: i.e. Get.reset() vs Get.delete(), correct me if I am wrong... – vigdora Jun 01 '22 at 07:53
  • 1
    `permanent` binds the object to the application lifecycle. Services are bound to the navigation stack. For most purposes, this is the same thing. However, if your application continues to run in the background, then `permanent` objects might not be cleared, but that is usually not the case for Flutter apps – MrMikimn Jun 01 '22 at 20:02
  • in my comment above yours I actually extended my question to GetxServices vs GetxController() , and not just simple object (as in my original question).[1] I think controllers are also binded to the navigation stack don't they? [2] so if the app is running in the background, services might be cleared? [3] why it is usually not the case for flutter apps? – vigdora Jun 02 '22 at 05:58