i'am building an app with flutter and firebase..(I'am new in flutter development). What I did is a system where the user can signup and signin. Once the user signup an email verification is sent to the user email account. I'll try to put all the step below
- signup
- redirect to email verification widget..(this check if user has verified the email with a Timer) if yes, navigator push new Page (HomePage).
the main logic is this one.
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "My App Name",
debugShowCheckedModeBanner: false,
home: AuthService().handleAuth(),
theme: ThemeData(
visualDensity: VisualDensity.adaptivePlatformDensity
),
);
}}
this is what the AuthService().handleAuth()
does:
handleAuth() {
return StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (BuildContext context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
print(snapshot.hasData);
if (snapshot.hasData && emailVerificationNeeded() == false) {
return HomePage();
}
return LoginPage();
} else {
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}
},
);
}
The verifyPage check with a timer if user has verified the email
Future<void> checkEmailVerified() async {
user = auth.currentUser!;
await user.reload();
if (user.emailVerified) {
timer.cancel();
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) => HomePage()));
}
}
Until here everything work fine! Now for a test purpose in the HomePage there is a button that fire the following action
FirebaseAuth.instance.signOut();
if I click in the button and I logout the user still remain in the home page instead of going back to the LoginPage. This problem happen only the first time when the user is redirected in the Verification page. On all the other case if I'm verified and I'm logged in, once I click on the Logout button the user is redirect back to the Login page.
Any ideas?
Thank you all