2017-06-22 78 views
1

我正在嘗試使用Firebase和Kotlin進行註冊。 看看文檔,我看到了Java中的所有例子。所以當我嘗試在Kotlin中實現時,我無法使其工作。Firebase Android - 在Kotlin中使用電子郵件和密碼創建用戶

在Java中應該是這樣的:

// [START create_user_with_email] 
     mAuth.createUserWithEmailAndPassword(email, password) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         if (task.isSuccessful()) { 
          // Sign in success, update UI with the signed-in user's information 
          FirebaseUser user = mAuth.getCurrentUser(); 

         } else { 
          // If sign in fails, display a message to the user. 
          ...... 
         } 

         // [START_EXCLUDE] 
         ....... 
         // [END_EXCLUDE] 
        } 
       }); 
     // [END create_user_with_email] 

但是當我嘗試在科特林來實現這樣的:

// [START create_user_with_email] 
     mAuth.createUserWithEmailAndPassword(email, password) 
       .addOnCompleteListener(this, OnCompleteListener<AuthResult> { task -> 
        if (task.isSuccessful) { 
         // Sign in success, update UI with the signed-in user's information       
         val user = mAuth.currentUser      
        } else { 
         ...... 
        } 

        // [START_EXCLUDE] 
          ..... 
        // [END_EXCLUDE] 
       }) 
     // [END create_user_with_email] 

但是這一點,給我一個錯誤: enter image description here

我不知道如何解決它。

的例子是:https://github.com/firebase/quickstart-android/blob/master/auth/app/src/main/java/com/google/firebase/quickstart/auth/EmailPasswordActivity.java#L119-L137

回答

10

我已經實現了火力地堡登記下列方式電子郵件和密碼,它的工作原理:

this.firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task: Task<AuthResult> -> 
    if (task.isSuccessful) { 
     //Registration OK 
     val firebaseUser = this.firebaseAuth.currentUser!! 
    } else { 
     //Registration error 
    } 
} 
+0

完美!像魅力一樣工作;) – Shudy

+0

葉完美的解決方案:) –

相關問題