Im still new in Flutter , I wanted to know how to redirect to another page after login to my app using google sign in, can anyone help in this matter? Heres the code. Im not sure where to redirect to another page in this case. Thank you
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
class GoogleSignInProvider extends ChangeNotifier {
final googleSignIn = GoogleSignIn();
GoogleSignInAccount? _user;
GoogleSignInAccount get user => _user!;
Future googleLogin() async{
final googleUser = await googleSignIn.signIn();
if (googleUser == null) return;
_user = googleUser;
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
notifyListeners();
}
}
4
Answers
The method FirebaseAuth.instance.signInWithCredential returns a Future(Promise) and it can either be that the user successfully signed in or not.
If you want to move to a difference screen, you can just use Navigator.push method:
Documentation
Firebase auth offers a
authStateChanges()
which is aStream
, so every time a new user logs in or logged out, it will get triggered, in your code, calling:will trigger it if it ran successfully.
you can listen to that
authStateChanges()
stream by using aStreamBuilder
like in this example:This widget should run on a top level of your app ( as an example using it in the
home
of theMaterialApp
of your app)initially, when there is no user logged in, the firebase’s
User
will benull
, so it will redirect to theLoginPage
on a successfully logged-in operation such as the
FirebaseAuth.instance.signInWithCredential(credential);
in your code, theUser
will have some data and it will not benull
, so theStreamBuilder
will get notified, and show theHomePage()
page.Here’s what I did with mine
then in the login screen you could add this to navigate if you logged in successfully:
I write mine based on this article
You can go to next screen as below code after click on google login button from login page