Implementation of Firebase Auth with Flutter and Provider


I am a bit confused about the implementation of Provider with Firebase Auth. Every tutorial seems to use different methods.

I have a service named AuthService which contains a variable final FirebaseAuth auth = FirebaseAuth.instance. So, I want to use the following stream: AuthService.auth.onAuthStateChanged in a widged. The question is should I use something like:

  value: AuthService().auth.onAuthStateChanged,
  child: ...

or create a constructor like this:

  create: (_) => AuthService(),
  child: ...

and access it somewhere in the widget:

final authService = Provider.of<AuthService>(context);<br>
final user = authService.auth.onAuthStateChanged;


You can use similar to this thing, If user is logged in he will lead to MainScreen() otherwise it will lead you to LoginPage(), you don’t need to use

Widget _handleAuth() {
    return StreamBuilder<FirebaseUser>(
      stream: FirebaseAuth.instance.onAuthStateChanged,
      builder: (BuildContext context, snapshot) {
        return (!snapshot.hasData)
            ? LoginPage()
            : MainScreen();

Answered By – user13615795

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *