使用vccode在Flutter上使用异常Firebase Auth

时间:2019-04-20 01:22:49

标签: dart flutter

我正在使用Firebase,Flutter和vscode开发登录系统。

我想知道如何处理Firebase生成的异常。 如果EMAIL已经注册。

当前正在产生错误:

Exception has occurred.
PlatformException (PlatformException(ERROR_EMAIL_ALREADY_IN_USE, The email address is already in use by another account., null))

如果电子邮件已经注册,我想通知用户。

代码:

Future<void> signUp({@required Map<String, dynamic> userData,@required String pass,@required VoidCallback onSuccess,@required VoidCallback onFail}) async{
    isLoading = true;
    notifyListeners();

    _auth.createUserWithEmailAndPassword(
      email: userData["email"],
      password: pass
    ).then((user) async{
      firebaseUser = user;
      await _saveUserData(userData);
      onSuccess();
      isLoading = false;
      notifyListeners();
    }).catchError((e){

      print(e);
      onFail();
      isLoading = false;
      notifyListeners();
    });

  }

2 个答案:

答案 0 :(得分:2)

如果要在发出ERROR_EMAIL_ALREADY_IN_USE时执行后续操作。

我认为捕获PlatformException并用code分支该过程是一个好主意,如下所示。

try {
  final result = await _auth.createUserWithEmailAndPassword(
      email: email,
      password: password,
  );
} on PlatformException catch (exception) {
  switch (exception.code) {
  case 'ERROR_EMAIL_ALREADY_IN_USE':
    // do something...
  default:
    break;
}

答案 1 :(得分:0)

使用on PlatformException catch (e)(e.message == 'ERROR_EMAIL_ALREADY_IN_USE')来处理这种情况。