I created a login with google in a flutter application, at the moment that works, but now I need to click on the "login" button to send me to a new page and in that page show the photo that my google account provides me. But every time I do it it gives me the following error:
The getter 'photoUrl' was called on null.
This would be the code the class calls loggedPage
class LogueadoPage extends StatefulWidget {
@override
_LogueadoPageState createState() => _LogueadoPageState();
}
class _LogueadoPageState extends State<LogueadoPage> {
FirebaseAuth _auth = FirebaseAuth.instance;
FirebaseUser _user;
GoogleSignIn _googleSignIn = new GoogleSignIn();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Google Logueado"),
),
body: Center(
child: CircleAvatar(
backgroundImage: NetworkImage(_user.photoUrl),
),
));
}
and from the login page I control everything like this: //the login design
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
FirebaseAuth _auth = FirebaseAuth.instance;
FirebaseUser _user;
GoogleSignIn _googleSignIn = new GoogleSignIn();
class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("google Authentication"),
),
body:Center(
child: OutlineButton(
onPressed: () {
handleSignIn().whenComplete(() => Navigator.of(context).pushNamed('pagina'));
},
child: Text("SignIn with Goolge"),
),
));
}
//login method
Future<void> handleSignIn() async {
final GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
AuthCredential credential = GoogleAuthProvider.getCredential(
idToken: googleSignInAuthentication.idToken,
accessToken: googleSignInAuthentication.accessToken);
AuthResult result = (await _auth.signInWithCredential(credential));
print(result);
_user = result.user;
setState(() {
isSignIn = true;
});
}
Basically you have to pass the user to the second route. In your code you need to do the following:
Have your function return the user.
In the button, call the function so that the user returns to you and pass it to your new route.
In the state class of the second route
state
you can access the user of the widgetThis is the most basic to make what you want work. For something more robust you need something like an inherited widget.