我正在使用Firebase Phone身份验证开发Kotlin应用程序。我很难实现这个verifyphonenumber。
private fun startPhoneNumberVerification(phoneNumber: String, mCallbacks: PhoneAuthProvider.OnVerificationStateChangedCallbacks?) {
Log.d("phoneNumber==", "" + phoneNumber);
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this@LoginActivity, // Activity (for callback binding)
mCallbacks)
}
实现上面的代码并获得错误(“以下任何函数都不能通过提供的aruguments调用”)以及“创建扩展函数PhoneAuthProvider?.verifyPhoneNumber”。有人可以指导我吗?
答案 0 :(得分:1)
我无法想到其他任何事情,所以我假设Firebase callbacks参数注释为非空的。
TL; DR:从你的callbacks参数中删除?
,或者当它为null时删除no-op,这样Kotlin就可以做一些神奇的类型推断。
答案 1 :(得分:0)
由于您确定回调不会为null,因为您将在onCreate中初始化它,将其声明为
lateinit var mCallbacks: PhoneAuthProvider.OnVerificationStateChangedCallbacks
这对我有用
答案 2 :(得分:-1)
class MainActivity : AppCompatActivity() {
var fbAuth = FirebaseAuth.getInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var btnLogin = findViewById<Button>(R.id.btnLogin)
btnLogin.setOnClickListener {view ->
signIn(view,"user@company.com", "pass")
}
}
fun signIn(view: View,email: String, password: String){
showMessage(view,"Authenticating...")
fbAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, OnCompleteListener<AuthResult> { task ->
if(task.isSuccessful){
var intent = Intent(this, LoggedInActivity::class.java)
intent.putExtra("id", fbAuth.currentUser?.email)
startActivity(intent)
}else{
showMessage(view,"Error: ${task.exception?.message}")
}
})
}
fun showMessage(view:View, message: String){
Snackbar.make(view, message, Snackbar.LENGTH_INDEFINITE).setAction("Action", null).show()
}
}