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

Flutter&Dart登录异常:点击一次却触发Too many requests错误求助

问题原因及解决办法

核心原因

出现"too many requests"的本质是短时间内触发了多次Firebase登录请求,大概率是以下两个原因:

  • 登录按钮未做请求状态控制,允许在异步请求未完成时重复点击(包括错误弹窗关闭后,之前的请求残留或重复触发)
  • 对话框关闭时的上下文处理不当,导致页面状态异常,间接触发重复请求

具体修复步骤

1. 添加登录状态锁,防止重复请求

在你的登录页面State类中添加加载状态变量,确保同一时间只有一个登录请求在执行:

bool _isLoading = false;

// 登录按钮的onPressed处理逻辑
onPressed: _isLoading ? null : () async {
  setState(() => _isLoading = true);
  try {               
    await FirebaseAuth.instance.signInWithEmailAndPassword(
      email: email,
      password: password,
    );

    Navigator.of(context).pushNamedAndRemoveUntil(
      notesRoute,
      (route) => false,
    );
  } on FirebaseAuthException catch (e) {
    devtools.log(e.code);
    await showErrorDialog(
      context,
      "Error: ${e.code}",
    );
  } catch (e) {
    devtools.log(e.toString());
    await showErrorDialog(
      context,
      "Error: ${e.toString()}",
    );
  } finally {
    setState(() => _isLoading = false);
  }
},

2. 修正对话框的上下文关闭逻辑

修改showErrorDialog中Navigator.pop的调用,使用rootNavigator: true确保正确关闭顶层对话框,避免上下文嵌套导致的状态异常:

Future<void> showErrorDialog(
  BuildContext context,
  String text,
) {
  return showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text("An error occured here!"),
          content: Text(text),
          actions: [
            TextButton(
              onPressed: () {
                // 使用rootNavigator确保关闭正确的对话框
                Navigator.of(context, rootNavigator: true).pop();
              },
              child: const Text("Ok"),
            )
          ],
        );
      });
}

3. 额外排查点

  • 检查是否有其他触发登录的逻辑(比如表单的onSubmitted事件),避免同时触发按钮点击和回车提交
  • 确保输入的邮箱/密码控件在错误弹窗关闭后,没有自动触发提交行为

完成以上修改后,建议冷启动项目(而非热重启),即可解决单次点击却触发多次请求的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 07:00:24