skip to Main Content

In this Project I’m using firebase authentication and for changing homepage of the MaterialApp a StreamBuilder is implemented at MaterialApp‘s home section but its not working as expected.

home of the MaterialApp,

  home: StreamBuilder(
            stream: FirebaseAuth.instance.authStateChanges(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                Constants.firebaseUser = snapshot.data!;

                return FutureBuilder(
                    future: FirebaseServices.getUserType(snapshot.data!.uid),
                    builder: (context, snap) {
                      if (snap.data == UserType.doctor) {
                        return const DoctorHomePage();
                      } else if (snap.data == UserType.patient) {
                        return const UserHomePage();
                      } else if (snap.data == UserType.admin) {
                        return const AdminHome();
                      } else {
                        return const LoadingSplash();
                      }
                    });
              } else {
                return const LoginPage();
              }
            }));

It should change the homepage from login to any of those pages at the time of successful login.
But it only navigating to those home page only after restart.
PS. here I’m using another future function to find the category in which the user falls in.

2

Answers


  1. You can use getxcontroller

     class AuthController extends GetxController {
    
    
     late Rx<User?> _user;
      FirebaseAuth auth = FirebaseAuth.instance;
    
    
    @override
      void onReady() {
        super.onReady();
        _user = Rx<User?>(auth.currentUser);
        _user.bindStream(auth.userChanges());
        ever(_user, initialScreen);
      }
    
          initialScreen(User? user) {
            if (user == null) {
                //navigate to login screen
            } else {
                //navigate to home screen
            }
          }
    }
    
    Login or Signup to reply.
  2. There might be several reasons why your Firebase authentication stream isn’t working. Here are some troubleshooting measures you may take:

    Examine your Firebase settings: Check your Firebase setup, including the Firebase API key and the project ID. Also, ensure that authentication is enabled in your Firebase project.

    Examine your import statements: Check that you’ve included the Firebase authentication libraries in your code. This is usually firebase/auth and firebase/app.

    Examine your code for errors: Examine your code to ensure that there are no syntax mistakes or other problems that might be causing the stream to fail.

    Examine the Firebase console: Check the Firebase console to check if any errors have occurred.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search