登录时出现上述错误。即使我确保仅在检查当前用户是否为空之后才调用isEmailVerified()
,也会引发此异常。
我的authentication.dart
文件如下所示:
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
abstract class BaseAuth {
Future<String> signIn(String email, String password);
Future<String> signUp(String email, String password);
Future<FirebaseUser> getCurrentUser();
Future<void> sendEmailVerification();
Future<void> signOut();
Future<bool> isEmailVerified();
}
class Auth implements BaseAuth {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Future<String> signIn(String email, String password) async {
AuthResult result = await _firebaseAuth.signInWithEmailAndPassword(
email: email, password: password);
FirebaseUser user = result.user;
return user.uid;
}
Future<String> signUp(String email, String password) async {
AuthResult result = await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: password);
FirebaseUser user = result.user;
sendEmailVerification();
return user.uid;
}
Future<FirebaseUser> getCurrentUser() async {
FirebaseUser user = await _firebaseAuth.currentUser();
return user;
}
Future<void> signOut() async {
return _firebaseAuth.signOut();
}
Future<void> sendEmailVerification() async {
FirebaseUser user = await _firebaseAuth.currentUser();
user.sendEmailVerification();
}
Future<bool> isEmailVerified() async {
FirebaseUser user = await _firebaseAuth.currentUser();
return user.isEmailVerified;
}
}
在LoginPage
中,我正在这样检查:
if(auth.signIn(email, password)!=null)
{
if(auth.getCurrentUser()!=null)
{
if(auth.isEmailVerified() != null) {
Toast.show("Login Successful!", context, duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
Route route = MaterialPageRoute(builder: (context) => HomePage());
Navigator.pushReplacement(context, route);
}
}
}
我不知道该如何解决。任何帮助表示赞赏。
答案 0 :(得分:1)
在您的Auth
类中,您的方法具有将来的签名。
您将不得不await
他们的结果,因为其他条件将需要它。
您可以这样做:
if((await auth.signIn(email, password))!=null){
if((await auth.getCurrentUser())!=null){
if((await auth.isEmailVerified()) != null) {
Toast.show("Login Successful!", context, duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
Route route = MaterialPageRoute(builder: (context) => HomePage());
Navigator.pushReplacement(context, route);
}
}
}
但是上面的代码段必须放在async
方法中。