I’m trying to bypass the Login
page by using a variable for the homepage widget
. I check FirebaseAuth
to determine if the user is signed in already. Then I set the homepage based on yes or no. But it gives me a blank white screen. Why doesn’t this work?
@override
Widget build(BuildContext context) {
Widget? home;
//Check to see if a user is already logged in.
FirebaseAuth.instance
.authStateChanges()
.listen((User? user) {
if (user == null) {
home = Signup();
} else {
home = const HomePage();
}
});
return MaterialApp(
navigatorKey: NavigationService.navigatorKey,
debugShowCheckedModeBanner: false, // turn off debug banner
title: 'My App',
home: home,
);
}
}
3
Answers
I think that you need to implement it before.
Here you a will have a complete guide to implement Firebase.
https://www.youtube.com/watch?v=rWamixHIKmQ
This is because, build method is not recall for firebaseauth user state changes. I’m not sure whether you use a stateful widget or stateless widget here as it wasn’t mentioned.
First of all, putting up a listener inside the build method is not recommended. This can lead to memory problems and unnecessary executions as, build method being called every time when state changes for stateful widgets, if you are using a stateful widget. For stateless widgets, build method will be called only once and your
home
variable change wont effectively rebuild the widget to display the new value.I recommend you to read through stateless widget and stateful widget differences and use
setState()
method to rerender the widget on value changes to get the screen updated.Since you have not provided much code to understand the issue.Here is full code to achieve your desired way.