繁体   English   中英

我想要动态错误消息而不是这个 static 错误消息

[英]I want dynamic error message instead of this static error message

我想要一个动态错误消息,而不是这个 static 错误消息。

请查看我的代码并帮助我解决在使用 Firebase 登录时在 Flutter 中显示错误消息的代码。

我正在为我的应用程序创建一个登录页面,并为此使用 Firebase 身份验证服务。 现在我试图在用户尝试使用错误的凭据或任何其他情况时显示错误对话框消息。 所以我已经完成了这个......我已经对此进行了编码并且它成功执行但是错误消息是static“发生意外错误”,而不是这个我想要一个动态错误消息,它特别显示登录凭据有什么问题,例如email是否格式错误或密码错误等。

我的代码:

class LoginPage extends StatefulWidget {
  final VoidCallback showRegisterPage;
  const LoginPage({Key? key, required this.showRegisterPage}) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final formKey = GlobalKey<FormState>(); //key for form
  String name = "";
  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();

  
  void showErrorMessage(Object error) {
  String errorMessage;
  if (error is String) {
    errorMessage = error;
  } else if (error is PlatformException) {
    errorMessage = error.message ?? 'An unknown error occurred';
  } else {
    errorMessage = 'An unexpected error occurred';
  }

  showDialog(
    context: context,
    builder: (context) {
      return AlertDialog(
        title: Text('Error'),
        content: Text(errorMessage),
        actions: <Widget>[
          ElevatedButton(
            child: Text('OK'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
  );
}
  
  
  Future LoginPage() async {
  try {
    await FirebaseAuth.instance.signInWithEmailAndPassword(
      email: _emailController.text.trim(),
      password: _passwordController.text.trim(),
    );
  } catch (error) {
    showErrorMessage(error);
  }
}

void showErrorMessage(对象错误){

检查此代码段以获取参考。

这是一种通用的Authentication Method ,但您可以根据需要进行修改。 有关代码列表,请参阅

Future<String> loginUser({
        required String email,
        required String password,
      }) async {
        String res = 'Some error occurred';
        try {
          if (email.isNotEmpty || password.isNotEmpty) {
            //Login user
            await _auth.signInWithEmailAndPassword(
                email: email, password: password);
            res = 'success';
          } else if (email.isEmpty && password.isEmpty) {
            res = 'Please enter email and password';
          } else if (email.isEmpty || password.isEmpty) {
            res = 'Please enter email and password';
          }
        } on FirebaseAuthException catch (err) {
          print(err);
          if (err.code == 'user-not-found') {
            res = 'No account associated with email address';
          } else if (err.code == 'wrong-password') {
            res = 'Password is incorrect';
          } else if (err.code == 'invalid-email') {
            res = 'Please enter valid email address';
          } else {
            res = 'Please enter valid email and password';
          }
        } catch (err) {
          res = err.toString();
        }
        return res;
      }

您需要捕获 firebase 给出的错误代码,然后检查抛出了什么错误,然后显示给用户。

可以使用类似的方法进行注册。 下面是一个错误处理的例子:

on FirebaseAuthException catch (err) {
      print(err);

      if (err.code == 'invalid-email') {
        res = 'The email address is badly formatted';
      } else if(err.code == 'unknown') {
        res = 'Please enter password';
      } else if (err.code == 'weak-password') {
        res = 'The password must be longer than 6 characters';
      } else if (err.code == 'email-already-in-use') {
        res = 'The email address already exists';
      } else {
        res = err.code.toString();
      }
    } catch (err) {
      res = err.toString();
      print(res);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM