Search code examples
flutterflutter-getx

Using getX in Flutter with a singleton service


Using getX in Flutter, suppose I need to use the same service for different controllers.

For example, the same DB service for both UsersController and ProductsController.

What would be a best practice to do that?

  • Creating a singleton DB service?
  • Using getIt with the DB service?
  • Some other getX trick?
  • GetxService?

Solution

  • It would be more accurate to create and use a singleton object with the following method.

    Get.put<LoginService>(LoginService(), permanent: true);
    
    Get.find<LoginService>();
    

    GetX Documentation about Get.put

    the class that you want to get to save, like a controller or anything

    // note: "S" means that it can be a class of any type