我正在尝试在我的flutter应用程序中使用Google的Firebase身份验证。但是,在处理用户登录时,我似乎找不到“代码”来区分我要处理的异常类型。
Loginhandler.dart:
class LoginHandler {
String signup(_username, _password) async {
bool _valid = false;
final prefs = await SharedPreferences.getInstance();
FirebaseAuth.instance.createUserWithEmailAndPassword(
email: _username, password: _password)
.catchError((e) {
print(e);
print(e.message);
print(e.code);
print(e.details);
});
...
错误输出:
W/BiChannelGoogleApi(26738): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@4e727e7
W/IInputConnectionWrapper(26738): getCursorCapsMode on inactive InputConnection
I/flutter (26738): PlatformException(exception, The email address is already in use by another account., null)
I/flutter (26738): The email address is already in use by another account.
I/flutter (26738): exception
I/flutter (26738): null
I/flutter (26738): PlatformException(exception, The given password is invalid. [ Password should be at least 6 characters ], null)
I/flutter (26738): The given password is invalid. [ Password should be at least 6 characters ]
I/flutter (26738): exception
I/flutter (26738): null
我已经按照this Stack Overflow线程突出显示我可以使用switch语句,但上面的错误输出没有错误“代码”供我使用。
我可以在Android上访问错误的“代码”属性。
try {
// login logic
} catch (e) {
print('Error: $e');
setState(() {
_isLoading = false;
if (_isIos) {
_errorMessage = e.details;
} else
_errorMessage = e.message;
print(e.code); // can access the code here
});
}
更新:
这是一个错误,它已经修复了这个PR https://github.com/flutter/plugins/pull/775。
根据我的经验,在iOS上有一个错误,你不能得到错误代码,只有错误文本消息。在Android上,错误代码可以正常工作。
在我的应用程序上,我最终检查错误文本消息。
The issue is already being tracked on flutter,你可以订阅它,并在修复时获得更新。
顺便说一句,this is the pull request that fixes it,但它还没有合并。