This is about Flutter Firebase Authentication plugin.
I am trying to send a verification email after creating a new user, but sendEmailVerification() internally uses currentUser(). This looks like a bug to me, but just in case, I am posting a question to stackoverflow. The error is the same on Android and IOS.
First two lines return FirebaseUser. The third returns null. Forth, if third is commented throws a null reference error.
Thanks,
Boris
user = await _auth.createUserWithEmailAndPassword(email: email, password: password);
// result is FirebaseUser
user = await _auth.signInWithEmailAndPassword(email: email, password: password);
// result is FirebaseUser
user = await _auth.currentUser();
// result is null
await user.sendEmailVerification();
//null reference, because it internally uses .currentUser()
Here is what I found, and I am no sure is it still a bug or not, but there is a way to make it work. FirebaseUser returned by signInWithEmailAndPassword is not really authenticated. Only user returned by onAuthStateChanged stream is. So, if you call user.sendEmailVerification() from onAuthStateChanged context, then everything is fine.
This will work:
_firebaseUserChanged = _auth.onAuthStateChanged.listen((FirebaseUser user) {
if (user != null && !user.isEmailVerified) {
user.sendEmailVerification();
}
});
Of course, this code is simplified. We do not want to send verification email on every login attempt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With