gpt4 book ai didi

dart - Flutter 上的异常 Firebase Auth,使用 vscode

转载 作者:行者123 更新时间:2023-12-03 03:26:11 25 4
gpt4 key购买 nike

我正在开发一个登录系统,使用 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();
});

}

最佳答案

如果您想在发出 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;
}

关于dart - Flutter 上的异常 Firebase Auth,使用 vscode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55769441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com