I have been trying to figure out the correct way to setup navigation through my app based on the value of FirebaseAuth (i.e: navigating the users only after they had logged in)
So I have created a scenario where I have 3 classes, I will start by listing out my test case and then will discuss the problems I am facing at the end.
User Class is the very first page in my app, it draws it is widgets after checking if there is a currentUser or not
class User extends StatefulWidget {
@override
_UserState createState() => new _UserState();
}
class _UserState extends State<User> {
FirebaseUser _user;
@override
initState() {
super.initState();
_checkUser();
}
@override
Widget build(BuildContext context) {
return _user == null? new SignInPage(): new HomePage();
}
_checkUser ()async {
FirebaseUser user = await FirebaseAuth.instance.currentUser();
setState((){
this._user = user;
});
}
}
When the app launches for the very first time, it loads SignInPage since there is no user data:
class SignInPage extends StatefulWidget {
@override
_SignInPageState createState() => new _SignInPageState();
}
class _SignInPageState extends State<SignInPage> {
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = new GoogleSignIn();
_handleSignIn() async{
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final FirebaseUser user = await _auth.signInWithGoogle(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
assert(await user.getToken() != null);
print (user);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Sign In"),
centerTitle: true,
leading: new Container(),
backgroundColor: new Color.fromARGB(255,17,165,137)),
body: new Container(
child: new Center(
child:
new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[new RaisedButton(
child: new Container(
child: new Row(children: <Widget>[
new Container(
child: new Image(
image: new NetworkImage(
"https://maxcdn.icons8.com/Share/icon/Logos//google_logo1600.png"),
),
margin: new EdgeInsets.only(right: 8.0)),
new Text("Login with Google", style: new TextStyle(color: Colors.redAccent)),
], mainAxisAlignment: MainAxisAlignment.center)),
onPressed: (){_handleSignIn().whenComplete((){Navigator.of(context).pushNamed("/Home");});}
,color: Colors.white),
],),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
)
],
)
)
)
);
}
}
This is my HomePage class, which is only accessible if the authentication is verified; my Home contains a RaisedButton on the center, and has a Textchild of the displayName of the current user, and an onPressed() action that calls the _signOut() method and returns to SignInage
final GoogleSignIn _googleSignIn = new GoogleSignIn();
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> {
FirebaseUser _currentUser;
initState() {
super.initState();
_myUser();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Hello:)"),
),
body: new Center(
child: new RaisedButton(child: new Text(_currentUser.displayName),
onPressed: (){_signOut().whenComplete((){Navigator.of(context).pushNamed("/SignInPage");});})
),
);
}
_signOut() async {
await FirebaseAuth.instance.signOut();
await _googleSignIn.signOut();
return "Signed out";
}
_myUser()async {
FirebaseUser user = await FirebaseAuth.instance.currentUser();
setState((){
this._currentUser = user;
});
}
}
The Problem:
When the app starts for the first time it loads
SignInPageas normal, however when I successfully sign in to my google account, it freezes without navigating me becausedisplayName was called on Null, from my understanding that flutter did not have enough time to prepare user data before the navigation occurs, I am guessing this because of what happens in points 2 and 3.When restarting the app and preserving the previous auth session, the app automatically starts with the
Homepage, and the displayName is correctly displayed.In the same session,I click the
RaisedButtonwhich signs out and correctly navigating me back toSignInPage, I try to log in again and we are back to the main problem, it tries to navigate me to myHomebefore thedisplayNameis obtained.
This is how I understand the problem, so what am I doing wrong here and how to design the correct flow?
P.S: the reason I want to do this is because I want to use some data of currentUser in my HomePage.
Update:
the console prints the following:
'package:test/SignInScreen.dart': Failed assertion: line 26: 'user.email != null': is not true.
This is in assert statements in _handleSignIn.
I have also tried to create a pre-Home page, same problem occurs, I am not sure what is wrong now.