如何在 Flutter 中定位 FirebaseAuthWeakPasswordException 的原因?

How to localize reason of FirebaseAuthWeakPasswordException in Flutter?

在 Flutter 中定位 FirebaseAuthWeakPasswordException 原因的最佳方法是什么?

official documentation 中提到了 getReason() 方法,但我无法找到我需要从该方法捕获和本地化的潜在原因列表之类的东西。唯一记录在案的弱密码的原因是最小长度为 6 个字符(也许这是唯一的原因?!)。

我也找不到 FirebaseAuthWeakPasswordException 的源代码 class and/or 使用它的源代码,也不是放入导入语句以导入 FirebaseAuthWeakPasswordException 的正确文件Google 搜索也没有帮助。所以,基本上我不知道从哪里开始(除了发送随机弱密码和检查响应,这不能保证我会涵盖所有可能的原因)。

在Flutter中Firebase弱口令异常的本地化问题,有什么思路/最佳实践吗?我想,我不是唯一想向用户解释为什么他们的密码没有被排除的人。

就跟这个模式一样...从

开始

on FirebaseAuthException catch (e)

  try {
      if (_emailT.text.trim().isNotEmpty &&
        _pasT.text.trim().isNotEmpty) {
        progress!.show();
   final _authenticatedUser =auth
            .signInWithEmailAndPassword(
              email: _emailT.text,
              password: _pasT.text);
        await _authenticatedUser.user!
          .reload();
        if (_authenticatedUser
          .user!.emailVerified) {
          await prefs.setBool(
            'loggedIn', true);
          progress.dismiss();
          if (_remember) {
            prefs.setBool('remember', true);
            prefs.setString(
              'shareEmail', _emailT.text);
            prefs.setString(
              'sharePass', _pasT.text);
          }
          Navigator.pushReplacement(
            context,
            MaterialPageRoute(
              builder: (context) =>
              VerifyStudent(),
            ),
          );
        } else {
          progress.dismiss();
          openCustomDialog(
            context: context,
            body:
            'your email have not been veriftied',
            heading: 'Verification');
        }
      } else {
        progress!.dismiss();
        ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(
            backgroundColor: Colors.blue,
            content: const Text(
              'All filed must not be empty'),
              duration:
        const Duration(seconds: 2),
                                                ));
      }
    } on FirebaseAuthException catch (e) {
      progress!.dismiss();
      if (e.code == 'invalid-email') {
        ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(
            backgroundColor: Colors.blue,
            content:
                                                      const Text('invalid-email'),
          duration:
        const Duration(seconds: 2),
                                                ));
      } else if (e.code ==
        'user-disabled') {
        ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(
            backgroundColor: Colors.blue,
            content:
                                                      const Text('user-disabled'),
          duration:
        const Duration(seconds: 2),
                                                ));
      } else if (e.code ==
        'user-not-found') {
        ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(
            backgroundColor: Colors.blue,
            content:
                                                      const Text('user-not-found'),
          duration:
        const Duration(seconds: 2),
                                                ));
      } else if (e.code ==
        'wrong-password') {
        ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(
            backgroundColor: Colors.blue,
            content:
                                                      const Text('wrong-password'),
          duration:
        const Duration(seconds: 2),
                                                ));
      }
    } catch (e) {
      progress!.dismiss();
      print(e);
    }

Class 文档中的描述:

Thrown when using a weak password (less than 6 chars) to create a new account or to update an existing account's password. Use getReason() to get a message with the reason the validation failed that you can display to your users.

getReason()方法只是returns一个字符串比异常体现了本质。并且在class描述中指定了本质。因此,无论何时收到 FirebaseAuthWeakPasswordException,您都可以确定它会发生,因为密码长度小于 6 个字符。

我对flutter不熟悉,但是这个案例的逻辑应该是这样的

if exception class is FirebaseAuthWeakPasswordException then
    show error with locale key = LocaleKey_ShortPassError
endif