使用 try/catch 创建类

问题描述 投票:0回答:1

我正在尝试通过创建一个使用 Google 电子邮件方法注册用户的类来重构我的代码,以便它更易于管理。

我有几个问题:

  1. 我想使用

    try/catch
    这样我就可以捕获任何错误。

  2. 我需要使用

    await
    ,但我收到此错误:

    await 表达式只能在异步函数中使用。 尝试用“async”或“async”标记函数体。*

下面是我尝试突破并创建一个新类的代码:

class RegisterUser {
  final _auth = FirebaseAuth.instance;
  bool registrationFail = false;
  String errorMessage = "";

  try {
    final newUser = await _auth.createUserWithEmailAndPassword(
        email: widget.email, password: widget.password);
    if (newUser != null) {
        ref
            .read(usersNotifierProvider.notifier)
            .updateuserID(newUser.user!.uid);
        ref
            .read(usersNotifierProvider.notifier)
            .updateEmail(newUser.user!.email!);

        /// Send the email verification
        Navigator.of(context).pushReplacement(MaterialPageRoute(
            builder: (context) => const VerifyEmailScreen()));
    } else {
      setState(() {
        registrationFail = true;
      });
    }
    setState(() {
      showSpinner = false;
    });
  } on FirebaseAuthException catch (error) {
    switch (error.code) {
      case "ERROR_INVALID_EMAIL":
      case "invalid-email":
        errorMessage =
            "Your email address appears to be malformed.";
        break;
      case "email-already-in-use":
        errorMessage = "Email is already in use.";
        break;
      case "ERROR_WRONG_PASSWORD":
      case "wrong-password":
        errorMessage = "Your password is wrong.";
        break;
      case "weak-password":
        errorMessage = "Weak password";
        break;
      case "ERROR_USER_NOT_FOUND":
      case "user-not-found":
        errorMessage = "User with this email doesn't exist.";
        break;
      case "ERROR_USER_DISABLED":
      case "user-disabled":
        errorMessage =
            "User with this email has been disabled.";
        break;
      case "ERROR_TOO_MANY_REQUESTS":
      case "too-many-requests":
        errorMessage = "Too many requests. Try again later.";
        break;
      case "ERROR_OPERATION_NOT_ALLOWED":
      case "operation-not-allowed":
        errorMessage =
            "Signing in with Email and Password is not enabled.";
        break;
      default:
        errorMessage =
            "An undefined Error happened. Please try again.";
    }

    if (errorMessage != "") {
      ScaffoldMessenger.of(context).showSnackBar((SnackBar(
        content: Center(
          child: Text(
            errorMessage,
            style: const TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.w900),
          ),
        ),
        behavior: SnackBarBehavior.floating,
        margin: EdgeInsets.only(
          bottom: MediaQuery.of(context).size.height - 100,
          left: 10,
          right: 10,
        ),
        backgroundColor: Colors.redAccent,
      )
      )
      ); 
    }
  }
}

如何为此创建一个类? 谢谢

flutter firebase dart
1个回答
0
投票

您的 try/catch 位于类的主体中,而不是函数中。不是如何创建类的。

class RegisterUser {
  RegisterUser() {
    _loadUser();
  }

  _loadUser() async {
    // Do your try catch
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.