我已经使用 firebase auth 设置了一个 flutter 应用程序,整个身份验证过程很好(登录、注册和注销),但是当我创建多个屏幕并使用 Navigator.pushReplacement 导航时,如果我尝试不使用该应用程序,该应用程序只会注销当我在导航后尝试导航到任何其他屏幕时,退出将停止工作。
我也尝试过pushReplacemnt和pop,它要么导航到登录屏幕而不注销,要么注销而不进入登录屏幕。我希望能够成功注销,然后重定向到登录屏幕并能够再次登录。
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');
}
},
),
],
),
],
),
),
);
}
}
您可以像这样使用
.whenComplete
或 .then
:
ListTile(
leading: const Icon(Icons.logout),
title: const Text('Log Out'),
onTap: () async {
try {
await FirebaseAuth.instance.signOut().whenComplete(
() => Navigator.pop(context),
);
} catch (e) {
print('Error signing out: $e');
}
},
),