i have setup a flutter app with firebase auth, the whole authentication process is good(sign in, sign up and sign out), however when i created multiple screens and navigated using Navigator.pushReplacement, the app only signs out if i attempt without navigating to any other screens, when i attempt after navigating the signout stops working.
i have tried pushReplacemnt and pop as well, it either navigates to login screen without signing out or signs out without going to login screen. i want to be able to sign out successfully then redirected to login screen and be able to login again.
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:vijayadwaja/pages/loginpage.dart';
import 'profile.dart';
import 'bulkorders.dart';
import 'family.dart';
import 'subscription.dart';
import 'orders.dart';
import 'settings.dart';
class MyDrawer extends StatelessWidget {
const MyDrawer({
super.key,
});
@override
Widget build(BuildContext context) {
return Drawer(
backgroundColor: Color.fromRGBO(207, 22, 22, 1),
child: Container(
color: Colors.white,
child: Column(
children: [
Container(
decoration: const BoxDecoration(
color: Color.fromRGBO(207, 22, 22, 1),
// borderRadius:
// BorderRadius.only(bottomRight: Radius.circular(30)),
),
child: Padding(
padding: const EdgeInsets.only(top: 60.0, bottom: 25.0),
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.grey[300],
child: const Icon(
Icons.person_2_rounded,
color: Color.fromRGBO(207, 22, 22, 1),
),
),
title: const Text(
'John Doe',
style: TextStyle(
fontWeight: FontWeight.w600, color: Colors.white),
),
subtitle: const Text(
'+91 12345 67890',
style: TextStyle(color: Colors.white70),
),
onTap: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return UserProfile();
},
),
);
},
),
),
),
Column(
children: [
const Divider(
color: Colors.white,
),
ListTile(
leading: const Icon(Icons.subscriptions_rounded),
title: const Text('Subscription'),
onTap: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return const Subscription();
},
),
);
},
),
ListTile(
leading: const Icon(Icons.inventory_2_rounded),
title: const Text('My Orders'),
onTap: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return const MyOrders();
},
),
);
},
),
ListTile(
leading: const Icon(Icons.local_shipping),
title: const Text('Bulk Orders'),
onTap: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return const BulkOrders();
},
),
);
},
),
ListTile(
leading: const Icon(Icons.family_restroom),
title: const Text('My Family'),
onTap: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return const MyFamily();
},
),
);
},
),
const Divider(color: Colors.black38),
ListTile(
leading: const Icon(Icons.notifications),
title: const Text('Notifications'),
onTap: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return const Settings();
},
),
);
},
),
ListTile(
leading: const Icon(Icons.settings),
title: const Text('Settings'),
onTap: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return const Settings();
},
),
);
},
),
const Divider(color: Colors.black38),
ListTile(
leading: const Icon(Icons.logout),
title: const Text('Log Out'),
onTap: () async {
try {
await FirebaseAuth.instance.signOut();
Navigator.pop;
// Navigator.pushReplacement(
// context,
// MaterialPageRoute(
// builder: (context) =>
// const LoginPage()),
// );
} catch (e) {
print('Error signing out: $e');
}
},
),
],
),
],
),
),
);
}
}
2
Answers
the issue was that after triggering signout, it was navigating directly to login page instead of checking the authentication state via stream builder. so navigating to a widget which returns streambuilder and checks the authentication state after signout was the fix.
You can use
.whenComplete
or.then
like this: