Search code examples
flutterflutter-getx

I need to separate the API initialization in a general place, then the rest of pages use the same instance


We use this way to open a connection to Firebse.

  FirebaseAuth firebaseAuth = FirebaseAuth.instance;

But when we use this API inside multiple pages we use singleton like this.

@registerModule
abstract class FirebaseInjectableModule {
  @lazySingleton
  FirebaseAuth get firebaseAuth => FirebaseAuth.instance;
}

How Could i use this this way with getX. I need to separate the API initialization in a general place, then the rest of pages use the same instance.


Solution

  • You could do something like this in your main method or use in bindings:

     Get.lazyPut<FirebaseAuth>(()=>FirebaseAuth.instance, fenix: true); // `fenix: true` is somewhat like singletons
    

    And then on your pages:

    final FirebaseAuth firebaseAuth = Get.find();
    

    To get the instance.