I have a problem to Get data From Firestore, in Java code we can do this :
DocumentReference docRef = db.collection("cities").document("SF");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
but in Kotlin, when I try to override the onComplete
function, it's unavailable. so, how I can get the "task"?
If you are using Android Studio 3, you can use it to convert Java code to Kotlin. Put the code of interest in a java file and from the menu bar select Code > Convert Java File to Kotlin File.
As an example, for this code that you posted, the result of the conversion is shown below:
import android.support.annotation.NonNull;
import android.util.Log;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
public class ConversionExample {
private static final String TAG = "ConversionExample";
public void getDoc() {
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference docRef = db.collection("cities").document("SF");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
}
}
The file converted to Kotlin:
import android.util.Log
import com.google.firebase.firestore.FirebaseFirestore
class ConversionExample {
fun getDoc() {
val db = FirebaseFirestore.getInstance()
val docRef = db.collection("cities").document("SF")
docRef.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
val document = task.result
if (document != null) {
Log.d(TAG, "DocumentSnapshot data: " + task.result.data)
} else {
Log.d(TAG, "No such document")
}
} else {
Log.d(TAG, "get failed with ", task.exception)
}
}
}
companion object {
private val TAG = "ConversionExample"
}
}
Please use "object" syntax for it: object notation
For example Java code:
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Handler code here.
Toast.makeText(this.MainActivity, "Button 1",
Toast.LENGTH_LONG).show();
}
});
Kotlin:
button1.setOnClickListener(object: View.OnClickListener {
override fun onClick(view: View): Unit {
// Handler code here.
Toast.makeText(this@MainActivity, "Button 1",
Toast.LENGTH_LONG).show()
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With