Flutter集成Firebase Auth后邮箱验证状态不更新问题求助
问题描述
我开发了一款集成Firebase Auth的Flutter应用,当用户未验证邮箱时会显示提示横幅,横幅上的重发验证邮件按钮功能正常,但用户点击邮件完成验证后,应用仍显示邮箱未验证,该如何解决?
横幅代码:
if(FirebaseAuth.instance.currentUser!.emailVerified == false) Container( height: 40, width: double.infinity, color: Colors.red, child: Row( children: [ const Padding( padding: EdgeInsets.symmetric(horizontal: 8.0), child: Icon(Icons.error, color: Colors.white), ), const Text("请确认你的邮箱地址", style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold)), const Spacer(), TextButton( onPressed: () async { FirebaseAuth.instance.currentUser!.sendEmailVerification(); CustomSnackBar.showSnackBar(context, "邮件已发送"); }, child: const Text("重新发送", style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold)), ), ], ),),
解决方案
这是因为Firebase Auth本地缓存的用户信息不会自动更新,currentUser里的emailVerified还是旧状态,需要手动刷新或监听状态变化来同步最新数据。
方法一:手动刷新用户信息
在用户可能完成验证的时机(比如返回应用时),调用reload()刷新用户数据,再重新获取用户实例:
// 刷新用户信息 await FirebaseAuth.instance.currentUser?.reload(); // 获取更新后的用户数据 final updatedUser = FirebaseAuth.instance.currentUser; // 此时updatedUser?.emailVerified就是最新的验证状态
方法二:监听用户状态实时更新
用userChanges()流监听用户信息的所有变化(包括邮箱验证状态),自动触发UI刷新:
@override Widget build(BuildContext context) { return StreamBuilder<User?>( stream: FirebaseAuth.instance.userChanges(), builder: (context, snapshot) { final user = snapshot.data; return Column( children: [ // 根据最新状态显示/隐藏横幅 if (user != null && !user.emailVerified) Container( height: 40, width: double.infinity, color: Colors.red, child: Row( children: [ const Padding( padding: EdgeInsets.symmetric(horizontal: 8.0), child: Icon(Icons.error, color: Colors.white), ), const Text("请确认你的邮箱地址", style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold)), const Spacer(), TextButton( onPressed: () async { await user.sendEmailVerification(); CustomSnackBar.showSnackBar(context, "邮件已发送"); }, child: const Text("重新发送", style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold)), ), ], ), ), // 其他页面内容 ], ); }, ); }
方法三:监听应用生命周期自动刷新
用户验证邮箱时通常会离开应用打开邮箱,当应用回到前台时自动刷新用户信息:
class _YourPageState extends State<YourPage> with WidgetsBindingObserver { @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { if (state == AppLifecycleState.resumed) { // 应用回到前台,刷新用户信息并更新UI FirebaseAuth.instance.currentUser?.reload(); setState(() {}); } } @override Widget build(BuildContext context) { final user = FirebaseAuth.instance.currentUser; return Column( children: [ if (user != null && !user.emailVerified) // 你的提示横幅 Container( height: 40, width: double.infinity, color: Colors.red, child: Row( children: [ const Padding( padding: EdgeInsets.symmetric(horizontal: 8.0), child: Icon(Icons.error, color: Colors.white), ), const Text("请确认你的邮箱地址", style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold)), const Spacer(), TextButton( onPressed: () async { await user.sendEmailVerification(); CustomSnackBar.showSnackBar(context, "邮件已发送"); }, child: const Text("重新发送", style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold)), ), ], ), ), // 其他内容 ], ); } }
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

