[英]Unhandled Exception: NoSuchMethodError: The getter 'isEmailVerified' was called on null
I am getting the above mentioned error when logging in. This exception is raised even after I am ensuring that the isEmailVerified()
is called only after checking whether the current user is null or not.登录时出现上述错误。即使在确保仅在检查当前用户是否为 null 后才调用isEmailVerified()
后,也会引发此异常。
My authentication.dart
file looks like below:我的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;
}
}
And inside the LoginPage
, I am checking like this:在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);
}
}
}
I don't know how to resolve this.我不知道如何解决这个问题。 Any help is appreciated.任何帮助表示赞赏。
From your Auth
class, your methods have future signatures.从您的Auth
class 中,您的方法具有未来的签名。
You will have to await
their results since it will be needed by the other conditions.您将不得不await
他们的结果,因为其他条件将需要它。
You can do:你可以做:
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);
}
}
}
But the snippet above must be placed in within an async
method.但是上面的代码片段必须放在async
方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.