Flutter Firebase认证currentUser()返回null [英] Flutter Firebase Authentication currentUser() returns null

查看:69
本文介绍了Flutter Firebase认证currentUser()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与Flutter Firebase身份验证插件有关.
我试图在创建新用户后发送验证电子邮件,但sendEmailVerification()内部使用currentUser().对我来说,这似乎是一个错误,但以防万一,我正在向stackoverflow发布问题.该错误在Android和IOS上相同.

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.

前两行返回FirebaseUser.第三个返回null.第四,如果注释了第三,则抛出空引用错误.

谢谢,
鲍里斯

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()

推荐答案

这是我发现的内容,但我不确定它是否仍然是错误,但是有一种方法可以使它起作用.signInWithEmailAndPassword返回的FirebaseUser尚未真正通过身份验证.只有onAuthStateChanged流返回的用户.因此,如果您从onAuthStateChanged上下文调用user.sendEmailVerification(),那么一切都很好.

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.

这将起作用:

_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.

这篇关于Flutter Firebase认证currentUser()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆