有关Flutter和Firebase身份验证的教程系列。克隆项目并运行后,在auth.dart和login_page.dart中删除了以下错误。我该如何解决?
不能将类型“ AuthResult”的值分配给类型“ FirebaseUser”的变量
代码如下:
class Auth implements BaseAuth {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Future<String> signIn(String email, String password) async {
FirebaseUser user = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
return user.uid;
}
Future<String> createUser(String email, String password) async {
FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
return user.uid;
}
,并且未为类“ BaseAuth”定义登录方法“ signInWithEmailAndPassword”中的错误。 代码如下:
Future<void> validateAndSubmit() async {
if (validateAndSave()) {
try {
final BaseAuth auth = AuthProvider.of(context).auth;
if (_formType == FormType.login) {
final String userId = await auth.signInWithEmailAndPassword(_email, _password);
print('Signed in: $userId');
} else {
final String userId = await auth.createUserWithEmailAndPassword(_email, _password);
print('Registered user: $userId');
}
widget.onSignedIn();
} catch (e) {
print('Error: $e');
}
}
}
答案 0 :(得分:1)
最近,firebase_auth软件包上已有更新,现在这些方法返回Future<AuthResult>
。如果您想要FirebaseUser
,则需要获取AuthResult.user
。
这样的事情应该适合您:
FirebaseUser user = (await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password)).user;