Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - How to do onCompleteListener to get data From Firestore?

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"?

like image 530
Galih laras prakoso Avatar asked Nov 10 '17 01:11

Galih laras prakoso


2 Answers

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"
    }
}
like image 126
Bob Snyder Avatar answered Sep 21 '22 15:09

Bob Snyder


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()
    }
})
like image 29
Maxim Firsoff Avatar answered Sep 22 '22 15:09

Maxim Firsoff