Flutter登录页Firebase Auth错误弹窗不显示问题求助
Flutter Firebase Auth登录错误弹窗不显示问题
开发Flutter登录页面时,集成Firebase Auth实现登录功能,遇到错误提示弹窗不显示的问题:输入错误邮箱或密码时,页面仅停留在登录页,无任何错误弹窗提示。已按建议检查Widget的mounted状态、异步操作间隙及BuildContext并更新代码,但问题仍未解决。
相关代码如下:
import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:authentication_signup/components/my_button.dart'; import 'package:authentication_signup/components/my_textfield.dart'; import 'package:authentication_signup/components/square_tile.dart'; class LoginPage extends StatefulWidget { const LoginPage({super.key}); @override State<LoginPage> createState() => _LoginPageState(); } class _LoginPageState extends State<LoginPage> { //text editing controllers final usernameController = TextEditingController(); final passwordController = TextEditingController(); //sign user in method void signUserIn() async { //loading circle showDialog(context: context, builder: (context) { return const Center( child: CircularProgressIndicator(), ); },); try { // Try signing in await FirebaseAuth.instance.signInWithEmailAndPassword( email: usernameController.text, password: passwordController.text, ); // Close the loading dialog only if the widget is still mounted if (mounted) { Navigator.pop(context); } } on FirebaseAuthException catch (e) { // Close the loading dialog only if the widget is still mounted if (mounted) { Navigator.pop(context); } // Handle error if (e.code == 'user-not-found') { // Show error to user wrongEmailMessage(); } else if (e.code == 'wrong-password') { // Show error to user wrongPasswordMessage(); } } catch (e) { // Handle any other exceptions and close the loading dialog if mounted if (mounted) { Navigator.pop(context); } print('Error: $e'); } } // Wrong email message pop-up void wrongEmailMessage() { if (mounted) { showDialog( context: context, builder: (context) { return const AlertDialog( title: Text('Incorrect email'), content: Text('No user found with that email.'), ); }, ); } } // Wrong password message pop-up void wrongPasswordMessage() { if (mounted) { showDialog( context: context, builder: (context) { return const AlertDialog( title: Text('Incorrect password'), content: Text('The password you entered is incorrect.'), ); }, ); } } }
已尝试的排查方向:
- 确保Widget处于mounted状态:在调用
Navigator.pop(context)或显示弹窗前检查mounted属性 - 异步间隙问题:使用mounted属性判断Widget是否仍在组件树中
- Flutter Build Context:确认调用Navigator或showDialog时使用正确的BuildContext
内容的提问来源于stack exchange,提问作者Ranita Biswas
相关产品推荐
相关产品推荐

