Flutter Firebase creating a useful instance

Viewed 82

should I call it by creating a new instance every time I need it, as in the firebase documents
like this = FirebaseAuth.instance.currentuser

or should I create this statically on another page and always call the same instance?
like this = static FirebaseAuth _auth = FirebaseAuth.instance
Page1:
_auth.currentuser
Page2:
_auth.currentuser

or should I define this as registerLazySingleton with the Get_it package in one place(I don't know get_it so this may be wrong)

Or if there is a more useful way, I would like to find out, isn't it harmful to create different instances all the time?

1 Answers

Calling FirebaseAuth.instance doesn't create a new instance. It just returns the current instance of the default [FirebaseApp] configured with the app.

Ideally, you wouldn't want to call FirebaseAuth.instance every time you need to access FirebaseUser object, but you can call it at least once per class to keep it clean. It shouldn't affect the performance of your app.

Related