在电子邮件中验证邮件时,不会调用onAuthStateChanged
问题内容:
当用户注册我的应用程序时,他会收到一封验证电子邮件。在onAuthStateChanged
当用户使用被创建章24
-listener被调用createUserWithEmailAndPassword
而不是电子邮件得到证实后。
我有一个单独的类来处理所有身份验证。以下方法是注册用户
Future<FirebaseUser> signUpUser(email, password) async {
final FirebaseUser user = await _auth.createUserWithEmailAndPassword(email: email, password: password);
assert (user != null);
assert (await user.getIdToken() != null);
return user;
}
使用此方法在我的StatefulWidget中调用此方法
void _signUpUser() async {
try {
await Auth().signUpUser(_email, _password)
..sendEmailVerification();
} catch (e) {
print(e.toString());
}
}
并以我onAuthStateChanged
的initState
方法设置StatefulWidget
FirebaseAuth.instance.onAuthStateChanged.listen((user) {
print("Auth State Changed!");
if (user.isEmailVerified) {
print("EMail verified!");
}
}
问题答案:
onAuthStatechanged
仅在用户登录或注销时触发,而不在电子邮件验证时触发。
根据文档
onAuthStatechanged
将观察者添加到用户登录状态的更改。
观察者将在以下情况下触发:
-
首次调用auth()。onAuthStateChanged()时。它将以初始Auth状态触发。如果用户正在从auth()。signInWithRedirect()操作返回,则观察者将在初始触发之前等待该操作解决。
-
新用户登录时。
-
当已经登录的用户退出。