getCurrentUser() functions error

依然范特西╮ 提交于 2019-12-24 00:36:29

问题


FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
currentFirebaseUser.getDisplayName();

this code works with all get functions when auth with google but when auth with facebook i get nullPointerException. i check for user using this code

 if (FirebaseAuth.getInstance().getCurrentUser() == null) {
        goToLoginScreen();
    }
    else{
        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    }

回答1:


The documentation for getCurrentUser() says that it will, in fact, return null if there is no current user (there is no one logged in). You should always fully expect this case to be a possibility.

If you want to know as soon as possible if there is a user logged in, you should instead use an AuthStateListener to check for a login event by calling getCurrentUser() in its onAuthStateChanged() to see if it returns non-null.



来源:https://stackoverflow.com/questions/46208917/getcurrentuser-functions-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!