You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用登出未跳转登录页及账号数据操作异常求助

问题与解决方案

问题描述

删除Firestore与Firebase中的数据后,Flutter应用点击登出按钮无法跳转至LoginPage,反而跳转到主页;点击其他按钮也无法进入登录页。需要实现正确的登出逻辑:存在用户数据时执行登出操作,无数据时显示错误提示。

用户提供代码

账号删除代码

onPressed: ()async {
  try{
    if (FirebaseAuth.instance.currentUser!=null) {
      await FirebaseAuth.instance.currentUser?.delete();
      FirebaseFirestore.instance.collection('NewUsers').doc(docId).delete();
      ScaffoldMessenger.of(context)
          .showSnackBar(const SnackBar(
          backgroundColor: Colors.white,
          duration: Duration(milliseconds: 2000),
          content: Text(
            "Account Deleted Successfully",
            style: TextStyle(
                fontSize: 15,
                color: Colors.red,
                fontWeight: FontWeight.w400),
            textAlign: TextAlign.center,
          )));
      Navigator.push(context,
          MaterialPageRoute(builder: (context) =>
              LoginPage()));
    }
    else{
      ScaffoldMessenger.of(context)
          .showSnackBar(const SnackBar(
          backgroundColor: Colors.white,
          duration: Duration(milliseconds: 2000),
          content: Text(
            "No Data Found to Delete",
            style: TextStyle(
                fontSize: 15,
                color: Colors.red,
                fontWeight: FontWeight.w400),
            textAlign: TextAlign.center,
          )));
    }
  }
  on FirebaseAuthException catch(e){
    Navigator.pop(context);
      ScaffoldMessenger.of(context)
          .showSnackBar(const SnackBar(
          backgroundColor: Colors.white,
          duration: Duration(milliseconds: 2000),
          content: Text(
            "Sensitive Operation.Please Re-Authenticate",
            style: TextStyle(
                fontSize: 15,
                color: Colors.red,
                fontWeight: FontWeight.w400),
            textAlign: TextAlign.center,
          )));
  }
},

登出功能代码

InkWell(
  onTap: () async {
    try {
      if (FirebaseAuth.instance.currentUser!=null) {
        HapticFeedback.heavyImpact();
        await FirebaseAuth.instance.signOut();
        ScaffoldMessenger.of(context)
            .showSnackBar(const SnackBar(
            shape: RoundedRectangleBorder(),
            content: Center(
                child: Text(
                  'Logged Out from Account',
                  style: TextStyle(
                      fontFamily: 'CourierPrime',
                      color: Colors.red,
                      fontWeight: FontWeight.w500),
                )),
            backgroundColor: Colors.white));
      }
      else {
        ScaffoldMessenger.of(context)
            .showSnackBar(const SnackBar(
            backgroundColor: Colors.white,
            duration: Duration(milliseconds: 2000),
            content: Text(
              "Cannot Logout because no Data Found",
              style: TextStyle(
                  fontSize: 15,
                  color: Colors.red,
                  fontWeight: FontWeight.w400),
              textAlign: TextAlign.center,
            )));
      }
    }
    on FirebaseAuthException catch(e){
      if (e.code=="permission-denied"){
        ScaffoldMessenger.of(context)
            .showSnackBar(const SnackBar(
            backgroundColor: Colors.white,
            content: Text(
              "The user does not have permission to execute the specified operation",
              style: TextStyle(
                  fontSize: 15,
                  color: Colors.red,
                  fontWeight: FontWeight.w400),
              textAlign: TextAlign.center,
            )));
      }
      else {
        ScaffoldMessenger.of(context)
            .showSnackBar(const SnackBar(
            backgroundColor: Colors.white,
            content: Text(
              "Cannot Perform Operation. Re-try After short Period",
              style: TextStyle(
                  fontSize: 15,
                  color: Colors.red,
                  fontWeight: FontWeight.w400),
              textAlign: TextAlign.center,
            )));
      }
    }
  }
)

解决方案

1. 补全登出后的页面跳转逻辑

当前登出代码执行signOut()后没有跳转LoginPage的代码,这是核心问题。添加跳转代码,推荐使用Navigator.pushReplacement替代push,清空当前路由栈,避免用户返回主页:

await FirebaseAuth.instance.signOut();
// 添加页面跳转代码
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginPage()));

2. 优化账号删除的操作顺序

当前代码先删除Auth用户再删除Firestore数据,若Auth用户删除后,后续操作可能因权限问题失败。调整顺序为先删Firestore数据,再删Auth用户:

// 先删除Firestore用户数据
await FirebaseFirestore.instance.collection('NewUsers').doc(docId).delete();
// 再删除Auth用户账号
await FirebaseAuth.instance.currentUser?.delete();

3. 全局监听用户状态(可选但推荐)

通过FirebaseAuth的authStateChanges监听用户登录状态,自动处理页面跳转,确保状态变化时页面同步更新:
在应用入口或根页面添加监听:

@override
void initState() {
  super.initState();
  FirebaseAuth.instance.authStateChanges().listen((User? user) {
    if (user == null) {
      // 用户未登录时自动跳转登录页
      Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginPage()));
    }
  });
}

4. 完善异常提示文本

将异常提示中的英文文本改为中文,提升用户体验,例如:

  • "Sensitive Operation.Please Re-Authenticate" → "敏感操作,请重新验证身份"
  • "The user does not have permission to execute the specified operation" → "用户无权限执行该操作"

内容的提问来源于stack exchange,提问作者AddieBuddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 05:43:19