Firebase在Helper类中返回null实例

时间:2018-01-06 09:48:59

标签: firebase kotlin firebase-authentication

class FirebaseHelper {


    companion object {

        private var mAuth: FirebaseAuth? = null

        fun getInstance(): FirebaseAuth? {
            if(mAuth == null ){
                mAuth == FirebaseAuth.getInstance()
            }
            return mAuth;
        }

        fun getCurrentUser(): FirebaseUser?{
            return getInstance()?.currentUser
        }
    }
}

这里FirebaseAuth.getInstance()总是返回null,我不明白为什么。如果我在

这样的活动中使用它
mAuth = FirebaseAuth.getInstance()

它返回firebase实例。我没有得到什么区别。我试图不将FirebaseHelper.getInstance()方法设为静态,但它也不起作用。

任何提示?

1 个答案:

答案 0 :(得分:2)

class FirebaseHelper {

    companion object {

        private var mAuth: FirebaseAuth? = null

        fun getInstance(): FirebaseAuth? {
            if(mAuth == null ){

                // here you should use "=" instead of "=="
                mAuth == FirebaseAuth.getInstance()
            }
            return mAuth;
        }

        fun getCurrentUser(): FirebaseUser?{
            return getInstance()?.currentUser
        }
    }
}