我收到错误消息“错误类型'AuthResult'不是强制类型转换中类型'FirebaseUser'的子类型”;当我尝试登录或注册时 [英] I'm getting an error "Error type 'AuthResult' is not a subtype of type 'FirebaseUser' in type cast" when I'm trying to login or signup
问题描述
我正在为我的大学项目制作一个Flutter应用程序,在其中添加登录和注册页面并通过Firebase对其进行身份验证,当我单击登录时,调试控制台会显示错误类型'AuthResult'为而不是强制类型转换中类型'FirebaseUser'的子类型。,当我在此错误后重新加载应用程序时,它成功登录。
I'm making a flutter app for my college project, where I'm adding a login and signup page and authenticating it via Firebase, and when I click login the debug console says "Error type 'AuthResult' is not a subtype of type 'FirebaseUser' in type cast" and when I reload the app after this error it successfully logs in.
在此之前,一切都工作得最好在此更新后将 firebase_auth 软件包更新为0.12.0,方法 signInWithEmailAndPassword()和 createUserWithEmailAndPassword()引发错误 AuthResult类型的值可以
尝试更改变量的类型,或将右侧类型强制转换为 FirebaseUser ,因此我添加了强制转换 as FirebaseUser 修复了错误,并成功构建了应用程序,但是当我单击登录名或创建帐户时,调试控制台表示错误类型'AuthResult'不是子类型o f在强制转换类型中键入 FirebaseUser
Everything was working best before the update of the firebase_auth package to 0.12.0 after this update, the methods "signInWithEmailAndPassword()" and "createUserWithEmailAndPassword()" throwing an error "A value of type 'AuthResult' can't be assigned to a variable of type 'FirebaseUser'. Try changing the type of the variable, or casting the right-hand type to 'FirebaseUser'", so I added a cast as FirebaseUser which fixed the error and the app was built successfully but when i clicked on login or create account, debug console said Error type 'AuthResult' is not a subtype of type 'FirebaseUser' in type cast
在更新firebase_auth 0.12.0之前的主登录并创建帐户功能代码
the main login and create account function code before the update of firebase_auth 0.12.0
Future<String> signIn(String email, String password) async {
FirebaseUser user = await
FirebaseAuth.instance.signInWithEmailAndPassword(
email: email, password: password);
return user.uid;
}
Future<String> createUser(String email, String password) async {
FirebaseUser user = await
FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email, password: password);
return user.uid;
}
以上代码工作正常,更新后
(firebase_auth 0.12.0)相同的代码开始引发此错误,
the above code was working fine, after the update (firebase_auth 0.12.0) the same code started throwing this error,
A value of type 'AuthResult' can't be assigned to a variable of type
'FirebaseUser'.
Try changing the type of the variable, or casting the right-hand type to
'FirebaseUser'.dart(invalid_assignment)
我通过如下所示铸造 FirebaseUser来修正错误
I fixed the error by casting "FirebaseUser" as shown below
Future<String> signIn(String email, String password) async {
FirebaseUser user = await
FirebaseAuth.instance.signInWithEmailAndPassword(
email: email, password: password) as FirebaseUser;
return user.uid;
}
Future<String> createUser(String email, String password) async {
FirebaseUser user = await
FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email, password: password) as FirebaseUser;
return user.uid;
}
此新代码在编译时没有引发错误,但是当我尝试登录或创建新帐户会在调试控制台
中引发错误错误类型'AuthResult'不是
类型的'FirebaseUser'类型的子类型cast
并且新创建的帐户已成功创建firebase,但该应用程序不会在下一页上显示,但是一旦我重新加载,它就会从登录并创建帐户(退出工作正常)后应该出现的页面开始
this new code didn't threw an error in compilation but when I try to login or create new account it throws an error in debug console Error type 'AuthResult' is not a subtype of type 'FirebaseUser' in type cast and the new created account is successfully created on firebase but the app doesn't go on the next page but as soon as i reload it starts with the page that should come after login and creation of account(sign out is working perfectly)
推荐答案
这是插件中的一项重大更改,在此处记录了该文件
https://pub.dev/packages/firebase_auth#0120
This is a breaking change in the plugin and its documented here https://pub.dev/packages/firebase_auth#0120
因此,您不应该进行任何类型的强制转换重构代码以采用新的更改:
So you shouldn't do any type of casting you just have to refactor your code to adopt the new changes :
FirebaseUser user = (await FirebaseAuth.instance.
signInWithEmailAndPassword(email: email, password: password))
.user;
这篇关于我收到错误消息“错误类型'AuthResult'不是强制类型转换中类型'FirebaseUser'的子类型”;当我尝试登录或注册时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!