skip to Main Content

I have a Login Controller that does the action of loging in (with Google)

class LoginController extends GetxController {
  final _googleSignin = GoogleSignIn();
  var googleAccount = Rx<GoogleSignInAccount?>(null);

  login() async {
    googleAccount.value = await _googleSignin.signIn();
  }

  logout() async {
    googleAccount.value = await _googleSignin.signOut();
  }
}

Then I have a Log In UI that shows everything

final controller = Get.put(LoginController());
  LoginIn({
    super.key,
    required String title,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Sign-In Page')),
        body: Center(
          child: Obx(() {
            if (controller.googleAccount.value == null) {
              return loginButton(); //the Login button to log in with google
            } else {
              return seeProfile(); //after Login you are then able to see your profile
            }
          }),
        ));
  }

And then ofcourse I have a HomePage
How do I go to the Home Page after a successful log in

I tried to use ChatGPT and I did watch a couple of Youtube Videos but their code is ofcourse different then my and that makes it hard to implement their solution

2

Answers


  1. I think checking the example in the google_sign_in‘s package on pub.dev might help you achieving what you want.

    In summary, I would suggest you to listen to changes to the user in the initState method and push a new page after signIn is successful:

    @override
      void initState() {
        super.initState();
        StreamSubscription<GoogleSignInAccount?> subscription = _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount? account) {
          if (account != null) {
            //push new screen
            Navigator.of(context).popAndPushNamed(...)
            subscription.cancel()
          }
        });
      }
    

    I couldn’t test this code, but it should give you a general idea on how to proceed. Again, check the link above for more information.

    Login or Signup to reply.
  2. A cleaner method would be to use Stream builder and subscribe to the auth stream in the main.dart file. Based on the value, navigate the user.

    ”’
    StreamBuilder<User?>(
    initialData: null,
    // stream: RepositoryProvider.of(context).authStream(),

        stream: FirebaseAuth.instance.authStateChanges(),
    
                builder: (context, snapshot) {
                  if (!snapshot.hasData &&
                      snapshot.connectionState == ConnectionState.waiting) {
                    return const Center(
                      child: CircularProgressIndicator(),
                    );
                  }
                  if (snapshot.hasData) {
                    return HomePage();
                    
                    
                  }
                  
                  return Container();
                });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search