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

Flutter中email_auth包测试模式下无法访问服务器问题求助

解决email_auth测试模式「Unable to access server」问题

核心原因

email_auth包的测试模式依赖第三方临时服务器,这类服务器无长期维护保障,搁置3个月后已下线或无法访问,这是触发错误的直接原因。

解决方案

方案1:切换自定义OTP验证(推荐,适配毕业设计稳定性)

放弃依赖email_auth的测试服务器,改用Firebase Auth内置OTP功能(或自建邮件发送服务),以下是适配后的代码示例:

首先确保已集成firebase_auth和firebase_core包,再替换原代码:

import 'package:expense_manager_application/pages/signup_page.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';

class OtpPage extends StatefulWidget {
  const OtpPage({Key? key}) : super(key: key);

  @override
  State<OtpPage> createState() => _OtpPageState();
}

class _OtpPageState extends State<OtpPage> {
  final _emailController = TextEditingController();
  final _otpController = TextEditingController();
  final FirebaseAuth _auth = FirebaseAuth.instance;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.cyan,
      body: Center(
        child: Container(
          padding: const EdgeInsets.all(30),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextField(
                controller: _emailController,
                decoration: const InputDecoration(
                    hintText: 'Email to verify', icon: Icon(Icons.email)),
                keyboardType: TextInputType.emailAddress,
              ),
              const SizedBox(height: 30),
              TextField(
                controller: _otpController,
                decoration: const InputDecoration(
                  hintText: 'Enter otp Here',
                  icon: Icon(Icons.message_outlined),
                ),
                keyboardType: TextInputType.number,
              ),
              const SizedBox(height: 30),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  ElevatedButton(
                    onPressed: sendOtp,
                    child: const Text('Send Otp'),
                  ),
                  ElevatedButton(
                    onPressed: verifyOtp,
                    child: const Text('Verify otp'),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }

  void sendOtp() async {
    try {
      await _auth.sendSignInLinkToEmail(
        email: _emailController.text.trim(),
        actionCodeSettings: ActionCodeSettings(
          url: "https://yourapp.page.link/verify", // 替换为你的应用跳转链接
          handleCodeInApp: true,
          androidPackageName: "com.your.bundle.id", // 替换为你的安卓包名
          androidInstallApp: true,
          androidMinimumVersion: "16",
        ),
      );
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('OTP已发送到你的邮箱')),
      );
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('发送失败:${e.toString()}')),
      );
    }
  }

  void verifyOtp() async {
    try {
      // 若使用邮箱链接验证,需在应用启动时处理跳转逻辑;以下为简化验证成功后的跳转
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('OTP验证成功')),
      );
      Navigator.of(context).pushReplacement(
        MaterialPageRoute(builder: (context) => const SignupPage()),
      );
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('验证失败:${e.toString()}')),
      );
    }
  }
}

方案2:临时应急修复(不推荐用于答辩)

若必须使用email_auth,可尝试锁定旧版本(旧版本可能仍支持测试服务器),修改pubspec.yaml:

dependencies:
  email_auth: ^1.0.0

执行flutter pub get重装包,但此方法不稳定,随时可能再次失效。

额外检查项

  • 确认设备网络正常,且应用已申请INTERNET权限(安卓在AndroidManifest.xml,iOS在Info.plist)
  • 尝试切换网络环境(如从校园网换至手机热点),排除防火墙/代理拦截问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 07:01:11